Simple mbed OS sockets example for mbed OS5 & W5500 SPI Ethernet controller. This is a quick example of a simple HTTP client program using the network-socket API that is provided as a part of mbed-os. The program brings up an underlying network interface, and uses it to perform an HTTP transaction over a TCPSocket.

Dependencies:   W5500Interface easy-connect

Fork of mbed-os-example-mbed5-sockets by mbed-os-examples

Result

  • Serial Terminal Log

/media/uploads/Bongjun/img021.png

Revision:
40:afef93b6d854
Parent:
13:ed9e4aa00044
Child:
47:08787ef063cb
--- a/main.cpp	Tue Mar 20 10:00:03 2018 +0000
+++ b/main.cpp	Tue Mar 27 12:45:08 2018 +0100
@@ -25,12 +25,20 @@
     char *buffer = new char[256];
 
     // Send an HTTP request
-    strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
+    strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\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);
+    int received = 0;
+    int remaining = 256;
+    int rcount = 0;
+    char *p = buffer;
+    while (0 != (received = socket.recv(p, remaining))) {
+        p += received;
+        rcount += received;
+        remaining -= received;
+    }
     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