UDP Echo Server example

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
emilmont
Date:
Wed Aug 01 13:13:54 2012 +0000
Revision:
2:6b1012d93eb2
Parent:
1:3f409cd0bede
Child:
5:0d4087d2486d
Child:
7:bd7f46b6fa23
Remove redundant UDPPacket class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:3e54841149df 1 #include "mbed.h"
emilmont 1:3f409cd0bede 2 #include "EthernetInterface.h"
emilmont 1:3f409cd0bede 3
emilmont 2:6b1012d93eb2 4 #define ECHO_SERVER_PORT 7
emilmont 2:6b1012d93eb2 5
emilmont 1:3f409cd0bede 6 int main (void) {
emilmont 1:3f409cd0bede 7 EthernetInterface eth;
emilmont 1:3f409cd0bede 8 eth.init(); //Use DHCP
emilmont 1:3f409cd0bede 9 eth.connect();
emilmont 1:3f409cd0bede 10 printf("IP Address is %s\n", eth.getIPAddress());
emilmont 1:3f409cd0bede 11
emilmont 1:3f409cd0bede 12 UDPSocket server;
emilmont 2:6b1012d93eb2 13 server.bind(ECHO_SERVER_PORT);
emilmont 1:3f409cd0bede 14
emilmont 2:6b1012d93eb2 15 Endpoint client;
emilmont 1:3f409cd0bede 16 char buffer[256];
emilmont 1:3f409cd0bede 17 while (true) {
emilmont 1:3f409cd0bede 18 printf("\nWait for packet...\n");
emilmont 2:6b1012d93eb2 19 int n = server.receiveFrom(client, buffer, sizeof(buffer));
emilmont 1:3f409cd0bede 20
emilmont 2:6b1012d93eb2 21 printf("Received packet from: %s\n", client.get_address());
emilmont 2:6b1012d93eb2 22 server.sendTo(client, buffer, n);
emilmont 1:3f409cd0bede 23 }
emilmont 1:3f409cd0bede 24 }