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 Jun 19 12:30:04 2018 +0100
Revision:
49:1923a727df5b
Parent:
48:f2739ac5cb01
Child:
56:02a6401ec508
ONME-3556: Use default network interface

.
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 0:17bd84fc5087 2
mbed_official 5:3e952c60d705 3 // Network interface
mbed_official 49:1923a727df5b 4 NetworkInterface *net;
mbed_official 0:17bd84fc5087 5
mbed_official 0:17bd84fc5087 6 // Socket demo
mbed_official 5:3e952c60d705 7 int main() {
mbed_official 47:08787ef063cb 8 int remaining;
mbed_official 47:08787ef063cb 9 int rcount;
mbed_official 47:08787ef063cb 10 char *p;
mbed_official 47:08787ef063cb 11 char *buffer = new char[256];
mbed_official 47:08787ef063cb 12 nsapi_size_or_error_t r;
mbed_official 47:08787ef063cb 13
mbed_official 5:3e952c60d705 14 // Bring up the ethernet interface
mbed_official 49:1923a727df5b 15 printf("Mbed OS Socket example\n");
mbed_official 48:f2739ac5cb01 16
mbed_official 48:f2739ac5cb01 17 #ifdef MBED_MAJOR_VERSION
mbed_official 49:1923a727df5b 18 printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
mbed_official 48:f2739ac5cb01 19 #endif
mbed_official 48:f2739ac5cb01 20
mbed_official 49:1923a727df5b 21 net = NetworkInterface::get_default_instance();
mbed_official 49:1923a727df5b 22
mbed_official 49:1923a727df5b 23 if (!net) {
mbed_official 49:1923a727df5b 24 printf("Error! No network inteface found.\n");
mbed_official 49:1923a727df5b 25 return 0;
mbed_official 49:1923a727df5b 26 }
mbed_official 49:1923a727df5b 27
mbed_official 49:1923a727df5b 28 r = net->connect();
mbed_official 47:08787ef063cb 29 if (r != 0) {
mbed_official 49:1923a727df5b 30 printf("Error! net->connect() returned: %d\n", r);
mbed_official 47:08787ef063cb 31 }
mbed_official 0:17bd84fc5087 32
mbed_official 0:17bd84fc5087 33 // Show the network address
mbed_official 49:1923a727df5b 34 const char *ip = net->get_ip_address();
mbed_official 49:1923a727df5b 35 const char *netmask = net->get_netmask();
mbed_official 49:1923a727df5b 36 const char *gateway = net->get_gateway();
mbed_official 13:ed9e4aa00044 37 printf("IP address: %s\n", ip ? ip : "None");
mbed_official 13:ed9e4aa00044 38 printf("Netmask: %s\n", netmask ? netmask : "None");
mbed_official 13:ed9e4aa00044 39 printf("Gateway: %s\n", gateway ? gateway : "None");
mbed_official 0:17bd84fc5087 40
mbed_official 0:17bd84fc5087 41 // Open a socket on the network interface, and create a TCP connection to mbed.org
mbed_official 5:3e952c60d705 42 TCPSocket socket;
mbed_official 49:1923a727df5b 43 r = socket.open(net);
mbed_official 47:08787ef063cb 44 if (r != 0) {
mbed_official 47:08787ef063cb 45 printf("Error! socket.open() returned: %d\n", r);
mbed_official 47:08787ef063cb 46 }
mbed_official 0:17bd84fc5087 47
mbed_official 47:08787ef063cb 48 r = socket.connect("api.ipify.org", 80);
mbed_official 47:08787ef063cb 49 if (r != 0) {
mbed_official 47:08787ef063cb 50 printf("Error! socket.connect() returned: %d\n", r);
mbed_official 47:08787ef063cb 51 }
mbed_official 47:08787ef063cb 52
mbed_official 47:08787ef063cb 53 // Send a simple http request
mbed_official 47:08787ef063cb 54 char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
mbed_official 47:08787ef063cb 55 nsapi_size_t size = strlen(sbuffer);
mbed_official 0:17bd84fc5087 56
mbed_official 47:08787ef063cb 57 // Loop until whole request send
mbed_official 47:08787ef063cb 58 while(size) {
mbed_official 47:08787ef063cb 59 r = socket.send(sbuffer+r, size);
mbed_official 47:08787ef063cb 60 if (r < 0) {
mbed_official 47:08787ef063cb 61 printf("Error! socket.connect() returned: %d\n", r);
mbed_official 47:08787ef063cb 62 goto disconnect;
mbed_official 47:08787ef063cb 63 }
mbed_official 47:08787ef063cb 64 size -= r;
mbed_official 47:08787ef063cb 65 printf("sent %d [%.*s]\n", r, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 40:afef93b6d854 66 }
mbed_official 47:08787ef063cb 67
mbed_official 47:08787ef063cb 68 // Receieve an HTTP response and print out the response line
mbed_official 47:08787ef063cb 69 remaining = 256;
mbed_official 47:08787ef063cb 70 rcount = 0;
mbed_official 47:08787ef063cb 71 p = buffer;
mbed_official 47:08787ef063cb 72 while (0 < (r = socket.recv(p, remaining))) {
mbed_official 47:08787ef063cb 73 p += r;
mbed_official 47:08787ef063cb 74 rcount += r;
mbed_official 47:08787ef063cb 75 remaining -= r;
mbed_official 47:08787ef063cb 76 }
mbed_official 47:08787ef063cb 77 if (r < 0) {
mbed_official 47:08787ef063cb 78 printf("Error! socket.recv() returned: %d\n", r);
mbed_official 47:08787ef063cb 79 goto disconnect;
mbed_official 47:08787ef063cb 80 }
mbed_official 47:08787ef063cb 81
mbed_official 13:ed9e4aa00044 82 printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 13:ed9e4aa00044 83
mbed_official 13:ed9e4aa00044 84 // The api.ipify.org service also gives us the device's external IP address
mbed_official 47:08787ef063cb 85 p = strstr(buffer, "\r\n\r\n")+4;
mbed_official 47:08787ef063cb 86 printf("External IP address: %.*s\n", rcount-(p-buffer), p);
mbed_official 47:08787ef063cb 87 delete[] buffer;
mbed_official 0:17bd84fc5087 88
mbed_official 47:08787ef063cb 89 disconnect:
mbed_official 0:17bd84fc5087 90 // Close the socket to return its memory and bring down the network interface
mbed_official 0:17bd84fc5087 91 socket.close();
mbed_official 0:17bd84fc5087 92
mbed_official 5:3e952c60d705 93 // Bring down the ethernet interface
mbed_official 49:1923a727df5b 94 net->disconnect();
mbed_official 0:17bd84fc5087 95 printf("Done\n");
mbed_official 0:17bd84fc5087 96 }