UDP Socket Hello World with Ethernet

Dependencies:   mbed mbed-rtos EthernetInterface

mbed 2 and mbed OS 5

This is an mbed 2 example. For an mbed-os example, please see:

[Repository '/teams/mbed-os-examples/code/mbed-os-example-sockets/docs/tip/' not found]

main.cpp

Committer:
Kojto
Date:
2014-05-14
Revision:
10:1992d8ec32d2
Parent:
7:dedf5dde9798

File content as of revision 10:1992d8ec32d2:

#include "mbed.h"
#include "EthernetInterface.h"
 
int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    
    UDPSocket sock;
    sock.init();
    
    Endpoint nist;
    nist.set_address("utcnist.colorado.edu", 37);
    
    char out_buffer[] = "plop"; // Does not matter
    sock.sendTo(nist, out_buffer, sizeof(out_buffer));
    
    char in_buffer[4];
    int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
    
    unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
    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);
    
    sock.close();
    
    eth.disconnect();
    while(1) {}
}