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.

Committer:
mbed_official
Date:
Tue Mar 27 12:45:08 2018 +0100
Revision:
40:afef93b6d854
Parent:
13:ed9e4aa00044
Child:
47:08787ef063cb
Merge branch 'mbed-os-5.8.0-oob'

* mbed-os-5.8.0-oob:
Updating mbed-os to mbed-os-5.8.0-rc2
Read until connection is properly closed.
Add build instructions.
Convert line-feeds for stdout.
Updating mbed-os to mbed-os-5.8.0-rc1

.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:17bd84fc5087 1 #include "mbed.h"
mbed_official 5:3e952c60d705 2 #include "EthernetInterface.h"
mbed_official 0:17bd84fc5087 3
mbed_official 5:3e952c60d705 4 // Network interface
mbed_official 5:3e952c60d705 5 EthernetInterface net;
mbed_official 0:17bd84fc5087 6
mbed_official 0:17bd84fc5087 7 // Socket demo
mbed_official 5:3e952c60d705 8 int main() {
mbed_official 5:3e952c60d705 9 // Bring up the ethernet interface
mbed_official 5:3e952c60d705 10 printf("Ethernet socket example\n");
mbed_official 5:3e952c60d705 11 net.connect();
mbed_official 0:17bd84fc5087 12
mbed_official 0:17bd84fc5087 13 // Show the network address
mbed_official 5:3e952c60d705 14 const char *ip = net.get_ip_address();
mbed_official 13:ed9e4aa00044 15 const char *netmask = net.get_netmask();
mbed_official 13:ed9e4aa00044 16 const char *gateway = net.get_gateway();
mbed_official 13:ed9e4aa00044 17 printf("IP address: %s\n", ip ? ip : "None");
mbed_official 13:ed9e4aa00044 18 printf("Netmask: %s\n", netmask ? netmask : "None");
mbed_official 13:ed9e4aa00044 19 printf("Gateway: %s\n", gateway ? gateway : "None");
mbed_official 0:17bd84fc5087 20
mbed_official 0:17bd84fc5087 21 // Open a socket on the network interface, and create a TCP connection to mbed.org
mbed_official 5:3e952c60d705 22 TCPSocket socket;
mbed_official 5:3e952c60d705 23 socket.open(&net);
mbed_official 13:ed9e4aa00044 24 socket.connect("api.ipify.org", 80);
mbed_official 13:ed9e4aa00044 25 char *buffer = new char[256];
mbed_official 0:17bd84fc5087 26
mbed_official 12:2e7466eba9a3 27 // Send an HTTP request
mbed_official 40:afef93b6d854 28 strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n");
mbed_official 13:ed9e4aa00044 29 int scount = socket.send(buffer, strlen(buffer));
mbed_official 13:ed9e4aa00044 30 printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 0:17bd84fc5087 31
mbed_official 13:ed9e4aa00044 32 // Recieve an HTTP response and print out the response line
mbed_official 40:afef93b6d854 33 int received = 0;
mbed_official 40:afef93b6d854 34 int remaining = 256;
mbed_official 40:afef93b6d854 35 int rcount = 0;
mbed_official 40:afef93b6d854 36 char *p = buffer;
mbed_official 40:afef93b6d854 37 while (0 != (received = socket.recv(p, remaining))) {
mbed_official 40:afef93b6d854 38 p += received;
mbed_official 40:afef93b6d854 39 rcount += received;
mbed_official 40:afef93b6d854 40 remaining -= received;
mbed_official 40:afef93b6d854 41 }
mbed_official 13:ed9e4aa00044 42 printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 13:ed9e4aa00044 43
mbed_official 13:ed9e4aa00044 44 // The api.ipify.org service also gives us the device's external IP address
mbed_official 13:ed9e4aa00044 45 const char *payload = strstr(buffer, "\r\n\r\n")+4;
mbed_official 13:ed9e4aa00044 46 printf("External IP address: %.*s\n", rcount-(payload-buffer), payload);
mbed_official 0:17bd84fc5087 47
mbed_official 0:17bd84fc5087 48 // Close the socket to return its memory and bring down the network interface
mbed_official 0:17bd84fc5087 49 socket.close();
mbed_official 13:ed9e4aa00044 50 delete[] buffer;
mbed_official 0:17bd84fc5087 51
mbed_official 5:3e952c60d705 52 // Bring down the ethernet interface
mbed_official 5:3e952c60d705 53 net.disconnect();
mbed_official 0:17bd84fc5087 54 printf("Done\n");
mbed_official 0:17bd84fc5087 55 }