mbed-os-examples / Mbed OS mbed-os-example-mbed5-sockets
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 // Network interface
00004 NetworkInterface *net;
00005 
00006 // Socket demo
00007 int main() {
00008     int remaining;
00009     int rcount;
00010     char *p;
00011     char *buffer = new char[256];
00012     nsapi_size_or_error_t result;
00013 
00014     // Bring up the ethernet interface
00015     printf("Mbed OS Socket example\n");
00016 
00017 #ifdef MBED_MAJOR_VERSION
00018     printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00019 #endif
00020 
00021     net = NetworkInterface::get_default_instance();
00022 
00023     if (!net) {
00024         printf("Error! No network inteface found.\n");
00025         return 0;
00026     }
00027 
00028     result = net->connect();
00029     if (result != 0) {
00030         printf("Error! net->connect() returned: %d\n", result);
00031         return result;
00032     }
00033 
00034     // Show the network address
00035     const char *ip = net->get_ip_address();
00036     const char *netmask = net->get_netmask();
00037     const char *gateway = net->get_gateway();
00038     printf("IP address: %s\n", ip ? ip : "None");
00039     printf("Netmask: %s\n", netmask ? netmask : "None");
00040     printf("Gateway: %s\n", gateway ? gateway : "None");
00041 
00042     // Open a socket on the network interface, and create a TCP connection to ifconfig.io
00043     TCPSocket socket;
00044     // Send a simple http request
00045     char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n";
00046     nsapi_size_t size = strlen(sbuffer);
00047 
00048     result = socket.open(net);
00049     if (result != 0) {
00050         printf("Error! socket.open() returned: %d\n", result);
00051     }
00052 
00053     result = socket.connect("ifconfig.io", 80);
00054     if (result != 0) {
00055         printf("Error! socket.connect() returned: %d\n", result);
00056         goto DISCONNECT;
00057     }
00058 
00059     // Loop until whole request sent
00060     while(size) {
00061         result = socket.send(sbuffer+result, size);
00062         if (result < 0) {
00063             printf("Error! socket.send() returned: %d\n", result);
00064             goto DISCONNECT;
00065         }
00066         size -= result;
00067         printf("sent %d [%.*s]\n", result, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00068     }
00069 
00070     // Receieve an HTTP response and print out the response line
00071     remaining = 256;
00072     rcount = 0;
00073     p = buffer;
00074     while (remaining > 0 && 0 < (result = socket.recv(p, remaining))) {
00075         p += result;
00076         rcount += result;
00077         remaining -= result;
00078     }
00079     if (result < 0) {
00080         printf("Error! socket.recv() returned: %d\n", result);
00081         goto DISCONNECT;
00082     }
00083     // the HTTP response code
00084     printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
00085 
00086     delete[] buffer;
00087 
00088 DISCONNECT:
00089     // Close the socket to return its memory and bring down the network interface
00090     socket.close();
00091 
00092     // Bring down the ethernet interface
00093     net->disconnect();
00094     printf("Done\n");
00095 }