SimpleSocket 1.0 examples

Dependencies:   EthernetNetIf SimpleSocket 1.0 mbed

examples/httpclient.cpp

Committer:
yamaguch
Date:
2013-02-04
Revision:
40:84182fc63956
Parent:
httpclient.cpp@ 39:108499af2b53

File content as of revision 40:84182fc63956:

#include "EthernetNetIf.h"
#include "SimpleSocket.h"

void httpclient() {
    EthernetNetIf eth;
    eth.setup();
    
    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.connected()) {
        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);
            }
        }
    }
}