Hiroshi Yamaguchi / Mbed 2 deprecated SimpleSocketExamples

Dependencies:   EthernetInterface SimpleSocket mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers httpclient.cpp Source File

httpclient.cpp

00001 #include "SimpleSocket.h"
00002 
00003 void httpclient() {
00004     printf("URL => ");
00005     char url[128];
00006     scanf("%s", url);
00007 
00008     char hostpart[64], host[64], path[128];
00009     int port = 80;
00010     sscanf(url, "http://%[^/]%s", hostpart, path);
00011     sscanf(hostpart, "%[^:]:%d", host, &port);
00012 
00013     ClientSocket socket(host, port);
00014     
00015     if (socket) {
00016         socket.printf("GET %s HTTP/1.0\r\n\r\n", path);
00017 
00018         while (socket) {
00019             if (socket.available()) {
00020                 char buf[128] = {};
00021                 socket.read(buf, sizeof(buf) - 1);
00022                 printf("%s", buf);
00023             }
00024         }
00025     }
00026 }