SimpleSocket 1.0 examples

Dependencies:   EthernetNetIf SimpleSocket 1.0 mbed

httpclient.cpp

Committer:
yamaguch
Date:
2011-09-12
Revision:
25:b3ab82301345
Parent:
Example/httpclient.cpp@ 24:9c7a2e830ef6
Child:
33:39d9cdf99de8

File content as of revision 25:b3ab82301345:

#include "mbed.h"
#include "SimpleSocket.h"

void httpclient(char *url) {
    printf("** httpclient, url = %s\n", 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.connected()) {
            if (socket.available()) {
                char buf[128];
                int len = socket.read(buf, sizeof(buf) - 1);
                buf[len] = '\0';
                printf(buf);
            }
        }
        printf("Done.\n");
    } else {
        printf("Connction failed.\n");
    }
}