void UDPSOCKET_BIND_ADDRESS_PORT() { #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE int count = fetch_stats(); for (int j = 0; j < count; j++) { TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state); } #endif UDPSocket *sock = new UDPSocket; if (!sock) { TEST_FAIL(); } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(get_interface())); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->bind(get_interface()->get_ip_address(), 80)); delete sock; #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE count = fetch_stats(); for (int j = 0; j < count; j++) { TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state); } #endif }
void UDPSOCKET_BIND_ADDRESS() { #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE int count = fetch_stats(); for (int j = 0; j < count; j++) { TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state); } #endif UDPSocket *sock = new UDPSocket; if (!sock) { TEST_FAIL(); } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); SocketAddress sockAddr = SocketAddress(NetworkInterface::get_default_instance()->get_ip_address(), 80); nsapi_error_t bind_result = sock->bind(sockAddr); if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported"); } else { TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, bind_result); } delete sock; #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE count = fetch_stats(); for (int j = 0; j < count; j++) { TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state); } #endif }
void udp_server_task(void const *argument) { DigitalOut indicator(LED1); UDPSocket server; server.bind(ECHO_SERVER_PORT); // printf("[udp_server_task] Start\r\n"); Endpoint client; char buffer[BUFFER_SIZE] = { 0 }; while (true) { //printf("[udp_server_task] Wait for packet...\r\n"); int n = server.receiveFrom(client, buffer, sizeof(buffer)); if (n > 0) { //printf("[udp_server_task] Received packet from: %s\r\n", client.get_address()); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n; buffer[buffer_string_end_index] = '\0'; //printf("[udp_server_task] Server received: %s\r\n", buffer); if (host_port == 0) { strcpy(host_address, client.get_address()); host_port = ECHO_SERVER_PORT + 1; //printf("[udp_server_task] Set host address and port: %s:%d\r\n", host_address, host_port); } // Dispatch data to client for sending to test HOST cli_serv_mutex.lock(); // LOCK // Push to datagram queue datagram_queue.push_front(std::string(buffer)); max_queue_len = datagram_queue.size() > max_queue_len ? datagram_queue.size() : max_queue_len; received_packets++; cli_serv_mutex.unlock(); // LOCK indicator = !indicator; } } }
int main (void) { MBED_HOSTTEST_TIMEOUT(20); MBED_HOSTTEST_SELECT(udpecho_server_auto); MBED_HOSTTEST_DESCRIPTION(UDP echo server); MBED_HOSTTEST_START("NET_5"); EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[BUFFER_SIZE] = {0}; printf("MBED: Waiting for packet...\r\n"); while (true) { int n = server.receiveFrom(client, buffer, sizeof(buffer)); if (n > 0) { //printf("Received packet from: %s\n", client.get_address()); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n; buffer[buffer_string_end_index] = '\0'; //printf("Server received: %s\n", buffer); server.sendTo(client, buffer, n); } } }
void connectionInit() { // Initialize sockets if(!receiver.create()) cout << "error creating server socket" << endl; if(!receiver.bind(RECEIVE_PORT)) cout << "error binding to port " << RECEIVE_PORT << endl; //receiver.set_non_blocking(true); receiver.set_timeout(0); // using recv() }
void ethSetup() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); udp.init(); udp.bind(5683); udp.set_blocking(false, 10000); }
/// Tries to get UDP address on non existing server, must time out int testNonExistingServer () { TestHelper helper; UDPSocket socket; socket.bind(); UDPEchoClient client(socket); client.result() = memFun (&helper, &TestHelper::callback); client.start ("127.0.0.1", 80, 1000); // Note: Port 80 won't respond bool hasResult = helper.waitForResult (2000); tcheck (hasResult, "Must callback for timeout"); tcheck (helper.result() == error::TimeOut, "Must timeout"); return 0; }
void UDPSOCKET_BIND_UNOPENED() { UDPSocket *sock = new UDPSocket; if (!sock) { TEST_FAIL(); } nsapi_error_t bind_result = sock->bind(1024); if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported"); } else { TEST_ASSERT_EQUAL(NSAPI_ERROR_NO_SOCKET, bind_result); } delete sock; }
void UDPSOCKET_BIND_ADDRESS_NULL() { UDPSocket *sock = new UDPSocket; if (!sock) { TEST_FAIL(); } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); nsapi_error_t bind_result = sock->bind(NULL, 1024); if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported"); } else { TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, bind_result); } delete sock; }
int testSflxEchoServer () { TestHelper helper; UDPSocket socket; socket.bind(); UDPEchoClient client(socket); client.result() = memFun (&helper, &TestHelper::callback); client.start (schnee::settings().echoServer, schnee::settings().echoServerPort, 1000); bool hasResult = helper.waitForResult (2000); tassert (hasResult, "Must callback"); if (helper.result() != NoError){ fprintf (stderr, "Shall give no error, but gave: %s\n", toString (helper.result())); fprintf (stderr, "Maybe you don't have a udp access to the net?\n"); return 1; } printf ("Outside address: %s, outside port: %d\n", client.address().c_str(), client.port()); return 0; }
// Test Existing server. Note: Must be started on port 1234 int testExistingServer () { TestHelper helper; UDPSocket socket; socket.bind(); UDPEchoClient client(socket); client.result() = memFun (&helper, &TestHelper::callback); client.start ("127.0.0.1", 1234, 1000); bool hasResult = helper.waitForResult (2000); tassert (hasResult, "Must callback"); if (helper.result() != NoError){ fprintf (stderr, "Warning: Localhost echo server seems not to run!"); return 0; // do not interpret as an error } tcheck1 (client.port() == socket.port()); tcheck1 (client.address() == "127.0.0.1"); return 0; }
int main (void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[256]; while (true) { printf("\nWait for packet...\n"); int n = server.receiveFrom(client, buffer, sizeof(buffer)); printf("Received packet from: %s\n", client.get_address()); server.sendTo(client, buffer, n); } }
int main (void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[BUFFER_SIZE] = {0}; while (true) { printf("Wait for packet...\n"); int n = server.receiveFrom(client, buffer, sizeof(buffer)); if (n > 0) { printf("Received packet from: %s\n", client.get_address()); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n; buffer[buffer_string_end_index] = '\0'; printf("Server received: %s\n", buffer); server.sendTo(client, buffer, n); } } }
//Na 30 seconden timed the socket out en zal er een socketTimeoutExeption worden aangeroepen. //Als je dit wilt aanpassen moet je dat veranderen in de prackticalsocket.cpp in de functie recvFRom int main() { UDPSocket sock; //Verander IP SocketAddress localadr("172.16.120.134", 8888, SocketAddress::UDP_SOCKET); sock.bind(localadr); SocketAddress remoteadr; cout << "UDP Server is gestart:" << endl; int32_t temp[5]= {1,2,3,4,5}; //Dit bericht wordt verstuurd mavlink_message_t msg = encodeLidarMessage(temp, COMMAND_DESTINATION::COMMAND_DESTINATION_ENUM_END, LIDAR_COMMAND_FUNCTIONS::LIDAR_INIT); while(true){ try{ sock.recvFrom(&msg, sizeof(mavlink_message_t), remoteadr); cout << "recieved: " << decodeLidarMessage(msg).Payload[0] << endl; //Opnieuw een bericht samen stellen for(int i=0; i< 6; i++){ temp[i] = decodeLidarMessage(msg).Payload[i] + 1; } msg = encodeLidarMessage( temp, COMMAND_DESTINATION::COMMAND_DESTINATION_ENUM_END, LIDAR_COMMAND_FUNCTIONS::LIDAR_INIT); sock.sendTo(&msg, sizeof(mavlink_message_t), remoteadr); cout << "sending: " << decodeLidarMessage(msg).Payload[0] << endl; cout << "from: " << remoteadr.getAddress() << ":" << remoteadr.getPort() << endl; sleep(1); }catch(SocketTimedOutException e){ continue; } } }
int main(int argc, char **argv) { UDPSocket sock; SocketAddress localadr("145.89.98.137", 2222, SocketAddress::UDP_SOCKET); sock.bind(localadr); SocketAddress remoteadr; sock.setTimeOut(2); int count = 0; while(true){ try{ cout << "waiting" << endl; sock.recvFrom(&count, sizeof(int), remoteadr); cout << "recieved: " << count << endl; cout << "from: " << remoteadr.getAddress() << ":" << remoteadr.getPort() << endl; count++; sock.sendTo(&count, sizeof(int), remoteadr ); }catch(SocketTimedOutException e){continue;} } }
void main(void) { //***************** BEGINNING OF ETHERNET SETUP [DONT EDIT] *****************// EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[256]; //***************** END OF ETHERNET SETUP **********************************// //***************** BEGINNING OF WIRELESS SETUP [DONT EDIT] *****************// uint8_t channel = 6; //Set the Channel. 0 is default, 15 is max mrf.SetChannel(channel); //Start the timer timer.start(); //***************** END OF WIRELESS SETUP **********************************// raiseAllPins(); dropAllPins(); speedChecker.attach(&speedLogic,0.005); //sets ticker for interrupts dcOUT=0; // turns DC motor off initially int cycles = 0; int flush = 0; int startChar =0; while(1) { dcPWM.write(dutyCycle); cycles = cycles-1; if(needsInput==true) { //What MBED is receiving from client? if(flush) { int lengthBuffer = strlen(buffer); for(int i = 0; i <lengthBuffer; i++) { buffer[i]='\0'; } } printf("\nWait for packet...\n\r"); lightShow.drawChar('@'); //this should draw the character on the screen int n = server.receiveFrom(client, buffer, sizeof(buffer)); printf("\nReceived packet...\n\r"); printf("\nReceived integer n...%i\n\r",n); buffer[n]='\0'; printf("\nyour word is: %s\n\r",buffer); server.sendTo(client, buffer, n); //echo protocol needsInput=false; cycles=n; /*revive code*/ sendDelay = slowSpeed; //pins dutyCycle = slowMotor; //motor dcOUT = 1; //turn on motor off=false; slow = true; startChar=0; } if(cycles<=0) { needsInput= true; slow = false; off=true; dcOUT = 0; //turn motor off sendDelay = slowSpeed; //pins dutyCycle = slowMotor; //for whenever it turns on after you turned it off using REDUCE } if(!off) { char character = getNextCharacter(startChar, buffer); startChar=1; printf("\nchar: %c\n\r",character); int pinBinary = braille.matchCharacter(character); if(braille.isNumber(character)) { handleNumberCase(character); } led1=1; lightShow.drawChar(character); //this should draw the character on the screen led1=0; wait_ms(sendDelay); sendNumber(pinBinary); printf("Pinbinary: %i\n\r",pinBinary); //***** ACKNOWLEDGE CODE ******// int recLength = rf_receive(rxbuffer,128); while(recLength<=0) { led2=1; recLength = rf_receive(rxbuffer,128); } led2=0; //***** END ACKNOWLEDGE CODE ******// }//end if motor stopped code dropAllPins(); }//end while loop }//end all code