TCP Socket Hello World with Ethernet

Dependencies:   mbed mbed-rtos EthernetInterface

Dependents:   PLC_client lanGpio_2v0

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 int main() {
00005     EthernetInterface eth;
00006     eth.init(); //Use DHCP
00007     eth.connect();
00008     printf("IP Address is %s\n", eth.getIPAddress());
00009     
00010     TCPSocketConnection sock;
00011     sock.connect("mbed.org", 80);
00012     
00013     char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
00014     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00015     
00016     char buffer[300];
00017     int ret;
00018     while (true) {
00019         ret = sock.receive(buffer, sizeof(buffer)-1);
00020         if (ret <= 0)
00021             break;
00022         buffer[ret] = '\0';
00023         printf("Received %d chars from server:\n%s\n", ret, buffer);
00024     }
00025       
00026     sock.close();
00027     
00028     eth.disconnect();
00029     
00030     while(1) {}
00031 }