UDP Socket application to read current time from network.

Files at this revision

API Documentation at this revision

Comitter:
deepikabhavnani
Date:
Mon Oct 23 21:26:36 2017 +0000
Commit message:
UDP Socket example - Initial commit

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
img/uvision.png Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Mon Oct 23 21:26:36 2017 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Mon Oct 23 21:26:36 2017 +0000
@@ -0,0 +1,10 @@
+### Getting started with the UDPSocket API ###
+ 
+UDP Example
+This application reads the current UTC time by sending UDP packet to 
+"time.nist.gov" address at port 37.
+ 
+**Note:** The current example is limited to the ethernet interface on supported 
+devices. To use the example with a different interface, you will need to modify 
+main.cpp and replace the EthernetInterface class with the appropriate interface.
+ 
\ No newline at end of file
Binary file img/uvision.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 23 21:26:36 2017 +0000
@@ -0,0 +1,48 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+ 
+// Network interface
+EthernetInterface net;
+ 
+// Time protocol implementation : Address: time.nist.gov UDPPort: 37  
+ 
+typedef struct {
+    uint32_t secs;         // Transmit Time-stamp seconds.
+}ntp_packet;
+ 
+int main() {
+    // Bring up the ethernet interface
+    printf("UDP Socket example\n");
+    if(0 != net.connect()) {
+        printf("Error connecting\n");
+        return -1;
+    }
+ 
+    // Show the network address
+    const char *ip = net.get_ip_address();
+    printf("IP address is: %s\n", ip ? ip : "No IP");
+        
+    UDPSocket sock(&net);
+    SocketAddress sockAddr;
+ 
+    char out_buffer[] = "time";
+    if(0 > sock.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer))) {
+        printf("Error sending data\n");
+        return -1;
+    }
+    
+    ntp_packet in_data;
+    int n = sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet));
+    in_data.secs = ntohl( in_data.secs ) - 2208988800;    // 1900-1970
+    printf("Time Received %lu seconds since 1/01/1900 00:00 GMT\n", 
+                        (uint32_t)in_data.secs);
+    printf("Time = %s", ctime(( const time_t* )&in_data.secs));
+    
+    printf("Time Server Address: %s Port: %d\n\r", 
+                               sockAddr.get_ip_address(), sockAddr.get_port());
+    
+    // Close the socket and bring down the network interface
+    sock.close();
+    net.disconnect();
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Oct 23 21:26:36 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#6e0d01cd13e8aca7bf4d697c3699ec9225386881