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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <string>
00002 #include "mbed.h"
00003 #include "easy-connect.h"
00004 #include "nsapi_dns.h"
00005 
00006 // Network interface
00007 NetworkInterface *net;
00008 
00009 // Socket demo
00010 int main() {
00011     int remaining;
00012     int rcount;
00013     char *p;
00014     char *buffer = new char[256];
00015     nsapi_size_or_error_t r;
00016 
00017     // Bring up the ethernet interface
00018     printf("Mbed OS Socket example\n");
00019 
00020 #ifdef MBED_MAJOR_VERSION
00021     printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00022 #endif
00023 
00024 //    net = NetworkInterface::get_default_instance();
00025 
00026     printf("Easy connect...\n");
00027     net = easy_connect(true);
00028     if (!net) {
00029         printf("Cannot connect to the network, see serial output");
00030         return 1;
00031     }
00032     printf("Connected to the network. Opening a socket...\n");
00033  
00034     if (!net) {
00035         printf("Error! No network inteface found.\n");
00036         return 0;
00037     }
00038 
00039     r = net->connect();
00040     if (r != 0) {
00041         printf("Error! net->connect() returned: %d\n", r);
00042     }
00043 
00044     // Show the network address
00045     const char *ip = net->get_ip_address();
00046     const char *netmask = net->get_netmask();
00047     const char *gateway = net->get_gateway();
00048     printf("IP address: %s\n", ip ? ip : "None");
00049     printf("Netmask: %s\n", netmask ? netmask : "None");
00050     printf("Gateway: %s\n", gateway ? gateway : "None");
00051 
00052     // Open a socket on the network interface, and create a TCP connection to mbed.org
00053     TCPSocket socket;
00054     r = socket.open(net);
00055     if (r != 0) {
00056         printf("Error! socket.open() returned: %d\n", r);
00057     }
00058     
00059     r = socket.connect("api.ipify.org", 80);
00060     //r = socket.connect("23.23.114.123", 80);
00061     
00062     if (r != 0) {
00063         printf("Error! socket.connect() returned: %d\n", r);
00064     }
00065 
00066     // Send a simple http request
00067     char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
00068     nsapi_size_t size = strlen(sbuffer);
00069 
00070     // Loop until whole request send
00071     while(size) {
00072         r = socket.send(sbuffer+r, size);
00073         if (r < 0) {
00074             printf("Error! socket.connect() returned: %d\n", r);
00075             goto disconnect;
00076         }
00077         size -= r;
00078         printf("sent %d [%.*s]\n", r, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00079     }
00080 
00081     // Receieve an HTTP response and print out the response line
00082     remaining = 256;
00083     rcount = 0;
00084     p = buffer;
00085 
00086     r = socket.recv(p, remaining);
00087     /*
00088     // modified source..a bit strnage...
00089     while (0 < (r = socket.recv(p, remaining))) {
00090         p += r;
00091         rcount += r;
00092         remaining -= r;
00093     }
00094     */
00095     if (r < 0) {
00096         printf("Error! socket.recv() returned: %d\n", r);
00097         goto disconnect;
00098     }
00099 
00100     p += r;
00101     rcount += r;
00102     remaining -= r;
00103 
00104     printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
00105 
00106     // The api.ipify.org service also gives us the device's external IP address
00107     p = strstr(buffer, "\r\n\r\n")+4;
00108     printf("External IP address: %.*s\n", rcount-(p-buffer), p);
00109     delete[] buffer;
00110 
00111 disconnect:
00112     // Close the socket to return its memory and bring down the network interface
00113     socket.close();
00114 
00115     // Bring down the ethernet interface
00116     net->disconnect();
00117     printf("Done\n");
00118 }