Simple code for comunication via TCP between the mbed and PC.

Dependencies:   EthernetInterface SimpleSocket mbed-rtos mbed

Fork of SimpleSocketExamples by Hiroshi Yamaguchi

examples/httpclient.cpp

Committer:
numeral369
Date:
2014-12-17
Revision:
1:016774025718
Parent:
0:6dc3cfd058c6

File content as of revision 1:016774025718:

#include "SimpleSocket.h"

void httpclient() {
    printf("URL => ");
    char url[128];
    scanf("%s", url);

    char hostpart[64], host[64], path[128];
    int port = 80;
    sscanf(url, "http://%[^/]%s", hostpart, path);
    sscanf(hostpart, "%[^:]:%d", host, &port);

    ClientSocket socket(host, port);
    
    if (socket) {
        socket.printf("GET %s HTTP/1.0\r\n\r\n", path);

        while (socket) {
            if (socket.available()) {
                char buf[128] = {};
                socket.read(buf, sizeof(buf) - 1);
                printf("%s", buf);
            }
        }
    }
}