![](/media/cache/profiles/ibmfp2.jpg.50x50_q85.jpg)
Alternative TCPSocket example using an HTTP GET to read a short "helloworld" text web page using a different server
Fork of ARMs demo HTTP socket demo. ARM's server was redirected and the demo was no longer working. An alternative server was setup and the code was modified to display the web page text in addition to just "200 OK". Only works for a very short web page - buffer only 400 characters but RAM is running out on the LPC1768 in the demo! Data read from the web page is read and parsed to control the mbed's 4 LEDs for a basic IoT demo.
Diff: main.cpp
- Revision:
- 0:6b383744246e
- Child:
- 1:965d7fb768b6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jan 19 11:46:10 2017 -0600 @@ -0,0 +1,38 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +// Network interface +EthernetInterface net; + +// Socket demo +int main() { + // Bring up the ethernet interface + printf("Ethernet socket example\n"); + net.connect(); + + // Show the network address + const char *ip = net.get_ip_address(); + printf("IP address is: %s\n", ip ? ip : "No IP"); + + // Open a socket on the network interface, and create a TCP connection to mbed.org + TCPSocket socket; + socket.open(&net); + socket.connect("developer.mbed.org", 80); + + // Send a simple http request + char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n"; + int scount = socket.send(sbuffer, sizeof sbuffer); + printf("sent %d [%.*s]\n", scount, 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]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + + // Close the socket to return its memory and bring down the network interface + socket.close(); + + // Bring down the ethernet interface + net.disconnect(); + printf("Done\n"); +}