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:
Mon Sep 03 08:45:04 2018 +0100
Revision:
58:4aeb931b8475
Parent:
56:02a6401ec508
Child:
64:a3dc04daaa2a
Stop on connection failure.

.
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 58:4aeb931b8475 31 return r;
mbed_official 47:08787ef063cb 32 }
mbed_official 0:17bd84fc5087 33
mbed_official 0:17bd84fc5087 34 // Show the network address
mbed_official 49:1923a727df5b 35 const char *ip = net->get_ip_address();
mbed_official 49:1923a727df5b 36 const char *netmask = net->get_netmask();
mbed_official 49:1923a727df5b 37 const char *gateway = net->get_gateway();
mbed_official 13:ed9e4aa00044 38 printf("IP address: %s\n", ip ? ip : "None");
mbed_official 13:ed9e4aa00044 39 printf("Netmask: %s\n", netmask ? netmask : "None");
mbed_official 13:ed9e4aa00044 40 printf("Gateway: %s\n", gateway ? gateway : "None");
mbed_official 0:17bd84fc5087 41
mbed_official 0:17bd84fc5087 42 // Open a socket on the network interface, and create a TCP connection to mbed.org
mbed_official 5:3e952c60d705 43 TCPSocket socket;
mbed_official 56:02a6401ec508 44 // Send a simple http request
mbed_official 56:02a6401ec508 45 char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
mbed_official 56:02a6401ec508 46 nsapi_size_t size = strlen(sbuffer);
mbed_official 56:02a6401ec508 47
mbed_official 49:1923a727df5b 48 r = socket.open(net);
mbed_official 47:08787ef063cb 49 if (r != 0) {
mbed_official 47:08787ef063cb 50 printf("Error! socket.open() returned: %d\n", r);
mbed_official 47:08787ef063cb 51 }
mbed_official 0:17bd84fc5087 52
mbed_official 47:08787ef063cb 53 r = socket.connect("api.ipify.org", 80);
mbed_official 47:08787ef063cb 54 if (r != 0) {
mbed_official 47:08787ef063cb 55 printf("Error! socket.connect() returned: %d\n", r);
mbed_official 56:02a6401ec508 56 goto DISCONNECT;
mbed_official 47:08787ef063cb 57 }
mbed_official 47:08787ef063cb 58
mbed_official 47:08787ef063cb 59 // Loop until whole request send
mbed_official 47:08787ef063cb 60 while(size) {
mbed_official 47:08787ef063cb 61 r = socket.send(sbuffer+r, size);
mbed_official 47:08787ef063cb 62 if (r < 0) {
mbed_official 56:02a6401ec508 63 printf("Error! socket.send() returned: %d\n", r);
mbed_official 56:02a6401ec508 64 goto DISCONNECT;
mbed_official 47:08787ef063cb 65 }
mbed_official 47:08787ef063cb 66 size -= r;
mbed_official 47:08787ef063cb 67 printf("sent %d [%.*s]\n", r, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 40:afef93b6d854 68 }
mbed_official 47:08787ef063cb 69
mbed_official 47:08787ef063cb 70 // Receieve an HTTP response and print out the response line
mbed_official 47:08787ef063cb 71 remaining = 256;
mbed_official 47:08787ef063cb 72 rcount = 0;
mbed_official 47:08787ef063cb 73 p = buffer;
mbed_official 47:08787ef063cb 74 while (0 < (r = socket.recv(p, remaining))) {
mbed_official 47:08787ef063cb 75 p += r;
mbed_official 47:08787ef063cb 76 rcount += r;
mbed_official 47:08787ef063cb 77 remaining -= r;
mbed_official 47:08787ef063cb 78 }
mbed_official 47:08787ef063cb 79 if (r < 0) {
mbed_official 47:08787ef063cb 80 printf("Error! socket.recv() returned: %d\n", r);
mbed_official 56:02a6401ec508 81 goto DISCONNECT;
mbed_official 47:08787ef063cb 82 }
mbed_official 47:08787ef063cb 83
mbed_official 13:ed9e4aa00044 84 printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 13:ed9e4aa00044 85
mbed_official 13:ed9e4aa00044 86 // The api.ipify.org service also gives us the device's external IP address
mbed_official 47:08787ef063cb 87 p = strstr(buffer, "\r\n\r\n")+4;
mbed_official 47:08787ef063cb 88 printf("External IP address: %.*s\n", rcount-(p-buffer), p);
mbed_official 47:08787ef063cb 89 delete[] buffer;
mbed_official 0:17bd84fc5087 90
mbed_official 56:02a6401ec508 91 DISCONNECT:
mbed_official 0:17bd84fc5087 92 // Close the socket to return its memory and bring down the network interface
mbed_official 0:17bd84fc5087 93 socket.close();
mbed_official 0:17bd84fc5087 94
mbed_official 5:3e952c60d705 95 // Bring down the ethernet interface
mbed_official 49:1923a727df5b 96 net->disconnect();
mbed_official 0:17bd84fc5087 97 printf("Done\n");
mbed_official 0:17bd84fc5087 98 }