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
- Committer:
- mbed_official
- Date:
- 2017-01-04
- Revision:
- 7:6b328c773e79
- Parent:
- 5:3e952c60d705
- Child:
- 12:2e7466eba9a3
File content as of revision 7:6b328c773e79:
#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"); }