UDP Socket Hello World with Ethernet

Dependencies:   mbed mbed-rtos EthernetInterface

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 int main() {
00005     EthernetInterface eth;
00006     eth.init(); //Use DHCP
00007     eth.connect();
00008     
00009     UDPSocket sock;
00010     sock.init();
00011     
00012     Endpoint nist;
00013     nist.set_address("utcnist.colorado.edu", 37);
00014     
00015     char out_buffer[] = "plop"; // Does not matter
00016     sock.sendTo(nist, out_buffer, sizeof(out_buffer));
00017     
00018     char in_buffer[4];
00019     int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
00020     
00021     unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
00022     printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes);
00023     
00024     sock.close();
00025     
00026     eth.disconnect();
00027     while(1) {}
00028 }