UDP Echo Server example

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
emilmont
Date:
Thu Jul 26 15:08:23 2012 +0000
Revision:
1:3f409cd0bede
Parent:
0:3e54841149df
Child:
2:6b1012d93eb2
First implementation

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 1:3f409cd0bede 4 int main (void) {
emilmont 1:3f409cd0bede 5 EthernetInterface eth;
emilmont 1:3f409cd0bede 6 eth.init(); //Use DHCP
emilmont 1:3f409cd0bede 7 eth.connect();
emilmont 1:3f409cd0bede 8 printf("IP Address is %s\n", eth.getIPAddress());
emilmont 1:3f409cd0bede 9
emilmont 1:3f409cd0bede 10 UDPSocket server;
emilmont 1:3f409cd0bede 11 server.bind(7);
emilmont 1:3f409cd0bede 12
emilmont 1:3f409cd0bede 13 char buffer[256];
emilmont 1:3f409cd0bede 14 while (true) {
emilmont 1:3f409cd0bede 15 printf("\nWait for packet...\n");
emilmont 1:3f409cd0bede 16
emilmont 1:3f409cd0bede 17 UDPPacket packet(buffer, sizeof(buffer));
emilmont 1:3f409cd0bede 18 int n = server.receiveFrom(packet);
emilmont 1:3f409cd0bede 19
emilmont 1:3f409cd0bede 20 printf("Received packet from: %s\n", packet.get_address());
emilmont 1:3f409cd0bede 21 packet.set_data(buffer, n);
emilmont 1:3f409cd0bede 22 server.sendTo(packet);
emilmont 1:3f409cd0bede 23 }
emilmont 1:3f409cd0bede 24 }