
A quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of mbed-os.
Dependencies: WizFi310Interface_Draft
Fork of mbed-os-example-mbed5-wifi by
Revision 24:323569a565ec, committed 2017-07-07
- Comitter:
- mbed_official
- Date:
- Fri Jul 07 10:00:03 2017 +0100
- Parent:
- 23:d894ce70a586
- Child:
- 25:f290e2005735
- Commit message:
- Made sure entire buffer is transmitted
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Jul 05 13:15:03 2017 +0100 +++ b/main.cpp Fri Jul 07 10:00:03 2017 +0100 @@ -81,22 +81,45 @@ void http_demo(NetworkInterface *net) { TCPSocket socket; + nsapi_error_t response; printf("Sending HTTP request to www.arm.com...\r\n"); // Open a socket on the network interface, and create a TCP connection to www.arm.com socket.open(net); - socket.connect("www.arm.com", 80); + response = socket.connect("www.arm.com", 80); + if(0 != response) { + printf("Error connecting: %d\r\n", response); + socket.close(); + return; + } // Send a simple http request char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; - int scount = socket.send(sbuffer, sizeof sbuffer); - printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); + nsapi_size_t size = sizeof sbuffer; + response = 0; + while(size) + { + response = socket.send(sbuffer+response, size); + if (response < 0) { + printf("Error sending data: %d\r\n", response); + socket.close(); + return; + } else { + size -= response; + // Check if entire message was sent or not + printf("sent %d [%.*s]\r\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); + } + } // Recieve a simple http response and print out the response line char rbuffer[64]; - int rcount = socket.recv(rbuffer, sizeof rbuffer); - printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + response = socket.recv(rbuffer, sizeof rbuffer); + if (response < 0) { + printf("Error receiving data: %d\r\n", response); + } else { + printf("recv %d [%.*s]\r\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + } // Close the socket to return its memory and bring down the network interface socket.close();