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.
main.cpp@73:03f443752d8a, 2019-04-15 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Apr 15 14:00:14 2019 +0100
- Revision:
- 73:03f443752d8a
- Parent:
- 72:47ae7d8d7321
add contribution file
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets
Who changed what in which revision?
User | Revision | Line number | New 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 | 64:a3dc04daaa2a | 12 | nsapi_size_or_error_t result; |
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 | 64:a3dc04daaa2a | 28 | result = net->connect(); |
mbed_official | 64:a3dc04daaa2a | 29 | if (result != 0) { |
mbed_official | 64:a3dc04daaa2a | 30 | printf("Error! net->connect() returned: %d\n", result); |
mbed_official | 64:a3dc04daaa2a | 31 | return result; |
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 | 72:47ae7d8d7321 | 42 | // Open a socket on the network interface, and create a TCP connection to ifconfig.io |
mbed_official | 5:3e952c60d705 | 43 | TCPSocket socket; |
mbed_official | 56:02a6401ec508 | 44 | // Send a simple http request |
mbed_official | 72:47ae7d8d7321 | 45 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\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 | 64:a3dc04daaa2a | 48 | result = socket.open(net); |
mbed_official | 64:a3dc04daaa2a | 49 | if (result != 0) { |
mbed_official | 64:a3dc04daaa2a | 50 | printf("Error! socket.open() returned: %d\n", result); |
mbed_official | 47:08787ef063cb | 51 | } |
mbed_official | 0:17bd84fc5087 | 52 | |
mbed_official | 72:47ae7d8d7321 | 53 | result = socket.connect("ifconfig.io", 80); |
mbed_official | 64:a3dc04daaa2a | 54 | if (result != 0) { |
mbed_official | 64:a3dc04daaa2a | 55 | printf("Error! socket.connect() returned: %d\n", result); |
mbed_official | 56:02a6401ec508 | 56 | goto DISCONNECT; |
mbed_official | 47:08787ef063cb | 57 | } |
mbed_official | 47:08787ef063cb | 58 | |
mbed_official | 64:a3dc04daaa2a | 59 | // Loop until whole request sent |
mbed_official | 47:08787ef063cb | 60 | while(size) { |
mbed_official | 64:a3dc04daaa2a | 61 | result = socket.send(sbuffer+result, size); |
mbed_official | 64:a3dc04daaa2a | 62 | if (result < 0) { |
mbed_official | 64:a3dc04daaa2a | 63 | printf("Error! socket.send() returned: %d\n", result); |
mbed_official | 56:02a6401ec508 | 64 | goto DISCONNECT; |
mbed_official | 47:08787ef063cb | 65 | } |
mbed_official | 64:a3dc04daaa2a | 66 | size -= result; |
mbed_official | 64:a3dc04daaa2a | 67 | printf("sent %d [%.*s]\n", result, 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 | 72:47ae7d8d7321 | 74 | while (remaining > 0 && 0 < (result = socket.recv(p, remaining))) { |
mbed_official | 64:a3dc04daaa2a | 75 | p += result; |
mbed_official | 64:a3dc04daaa2a | 76 | rcount += result; |
mbed_official | 64:a3dc04daaa2a | 77 | remaining -= result; |
mbed_official | 47:08787ef063cb | 78 | } |
mbed_official | 64:a3dc04daaa2a | 79 | if (result < 0) { |
mbed_official | 64:a3dc04daaa2a | 80 | printf("Error! socket.recv() returned: %d\n", result); |
mbed_official | 56:02a6401ec508 | 81 | goto DISCONNECT; |
mbed_official | 47:08787ef063cb | 82 | } |
mbed_official | 64:a3dc04daaa2a | 83 | // the HTTP response code |
mbed_official | 13:ed9e4aa00044 | 84 | printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer); |
mbed_official | 13:ed9e4aa00044 | 85 | |
mbed_official | 47:08787ef063cb | 86 | delete[] buffer; |
mbed_official | 0:17bd84fc5087 | 87 | |
mbed_official | 56:02a6401ec508 | 88 | DISCONNECT: |
mbed_official | 0:17bd84fc5087 | 89 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:17bd84fc5087 | 90 | socket.close(); |
mbed_official | 0:17bd84fc5087 | 91 | |
mbed_official | 5:3e952c60d705 | 92 | // Bring down the ethernet interface |
mbed_official | 49:1923a727df5b | 93 | net->disconnect(); |
mbed_official | 0:17bd84fc5087 | 94 | printf("Done\n"); |
mbed_official | 0:17bd84fc5087 | 95 | } |