UDP Echo Server example

Dependencies:   EthernetInterface mbed-rtos mbed

main.cpp

Committer:
emilmont
Date:
2012-07-26
Revision:
1:3f409cd0bede
Parent:
0:3e54841149df
Child:
2:6b1012d93eb2

File content as of revision 1:3f409cd0bede:

#include "mbed.h"
#include "EthernetInterface.h"

int main (void) {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
    UDPSocket server;
    server.bind(7);
    
    char buffer[256];
    while (true) {
        printf("\nWait for packet...\n");
        
        UDPPacket packet(buffer, sizeof(buffer));
        int n = server.receiveFrom(packet);
        
        printf("Received packet from: %s\n", packet.get_address());
        packet.set_data(buffer, n);
        server.sendTo(packet);
    }
}