NS example

Revision:
0:f40a402e1725
diff -r 000000000000 -r f40a402e1725 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 02 15:29:27 2017 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+// Network interface
+EthernetInterface net;
+
+// Socket demo
+int main() {
+    // Bring up the ethernet interface
+    printf("Ethernet socket example\n");
+    net.connect();
+
+    // Show the network address
+    const char *ip = net.get_ip_address();
+    const char *netmask = net.get_netmask();
+    const char *gateway = net.get_gateway();
+    printf("IP address: %s\n", ip ? ip : "None");
+    printf("Netmask: %s\n", netmask ? netmask : "None");
+    printf("Gateway: %s\n", gateway ? gateway : "None");
+
+    // Open a socket on the network interface, and create a TCP connection to mbed.org
+    TCPSocket socket;
+//UDPSocket udp;
+//udp.open(&net);
+//udp.bind(56830);
+//udp.sendto();
+//udp.recvfrom();
+    socket.open(&net);
+    socket.connect("api.ipify.org", 80);
+    char *buffer = new char[256];
+
+    // Send an HTTP request
+    strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
+    int scount = socket.send(buffer, strlen(buffer));
+    printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer);
+
+    // Recieve an HTTP response and print out the response line
+    int rcount = socket.recv(buffer, 256);
+    printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
+
+    // The api.ipify.org service also gives us the device's external IP address
+    const char *payload = strstr(buffer, "\r\n\r\n")+4;
+    printf("External IP address: %.*s\n", rcount-(payload-buffer), payload);
+
+    // Close the socket to return its memory and bring down the network interface
+    socket.close();
+    delete[] buffer;
+
+    // Bring down the ethernet interface
+    net.disconnect();
+    printf("Done\n");
+}