UDP Echo Server example

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 #define ECHO_SERVER_PORT   7
00005 
00006 int main (void) {
00007     EthernetInterface eth;
00008     eth.init(); //Use DHCP
00009     eth.connect();
00010     printf("\nServer IP Address is %s\n", eth.getIPAddress());
00011     
00012     UDPSocket server;
00013     server.bind(ECHO_SERVER_PORT);
00014     
00015     Endpoint client;
00016     char buffer[256];
00017     while (true) {
00018         printf("\nWaiting for UDP packet...\n");
00019         int n = server.receiveFrom(client, buffer, sizeof(buffer));
00020         buffer[n] = '\0';
00021         
00022         printf("Received packet from: %s\n", client.get_address());
00023         printf("Packet contents : '%s'\n",buffer);
00024         printf("Sending Packet back to Client\n");
00025         server.sendTo(client, buffer, n);
00026     }
00027 }