SimpleSocket 1.0 examples

Dependencies:   EthernetNetIf SimpleSocket 1.0 mbed

Files at this revision

API Documentation at this revision

Comitter:
yamaguch
Date:
Mon Feb 04 09:04:25 2013 +0000
Parent:
39:108499af2b53
Commit message:
changed SimpleSocket libary name to SimpleSocketV1

Changed in this revision

SimpleSocket.lib Show diff for this revision Revisions of this file
SimpleSocketV1.lib Show annotated file Show diff for this revision Revisions of this file
echoclient.cpp Show diff for this revision Revisions of this file
echoserver.cpp Show diff for this revision Revisions of this file
examples/echoclient.cpp Show annotated file Show diff for this revision Revisions of this file
examples/echoserver.cpp Show annotated file Show diff for this revision Revisions of this file
examples/findbuddy.cpp Show annotated file Show diff for this revision Revisions of this file
examples/httpclient.cpp Show annotated file Show diff for this revision Revisions of this file
examples/multicast.cpp Show annotated file Show diff for this revision Revisions of this file
examples/ntpclient.cpp Show annotated file Show diff for this revision Revisions of this file
examples/ntpclient2.cpp Show annotated file Show diff for this revision Revisions of this file
examples/supertweet.cpp Show annotated file Show diff for this revision Revisions of this file
examples/udpreceiver.cpp Show annotated file Show diff for this revision Revisions of this file
examples/udpsender.cpp Show annotated file Show diff for this revision Revisions of this file
examples/webcontroller.cpp Show annotated file Show diff for this revision Revisions of this file
examples/webserver.cpp Show annotated file Show diff for this revision Revisions of this file
findbuddy.cpp Show diff for this revision Revisions of this file
httpclient.cpp Show diff for this revision Revisions of this file
multicast.cpp Show diff for this revision Revisions of this file
ntpclient.cpp Show diff for this revision Revisions of this file
ntpclient2.cpp Show diff for this revision Revisions of this file
supertweet.cpp Show diff for this revision Revisions of this file
udpreceiver.cpp Show diff for this revision Revisions of this file
udpsender.cpp Show diff for this revision Revisions of this file
webcontroller.cpp Show diff for this revision Revisions of this file
webserver.cpp Show diff for this revision Revisions of this file
diff -r 108499af2b53 -r 84182fc63956 SimpleSocket.lib
--- a/SimpleSocket.lib	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/yamaguch/code/SimpleSocket/#3eb1ce25eac4
diff -r 108499af2b53 -r 84182fc63956 SimpleSocketV1.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleSocketV1.lib	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/yamaguch/code/SimpleSocketV1/#3eb1ce25eac4
diff -r 108499af2b53 -r 84182fc63956 echoclient.cpp
--- a/echoclient.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void echoclient() {
-    EthernetNetIf eth;
-    eth.setup();
-
-    printf("Echo server => ");
-    char server[32];
-    scanf("%s", server);
-
-    ClientSocket socket(server, 1234);
-
-    if (socket) {
-        char message[80] = {};
-        printf("Enter message => ");
-        int c = 0;
-        while (c < ' ' || 0x7E < c)
-            c = getc(stdin);
-        ungetc(c, stdin);
-        for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++)
-            message[i] = c;
-
-        socket.printf("%s", message);
-
-        // wait until data is received
-        while (!socket.available())
-            ;
-        printf("Received: ");
-
-        while (socket.available()) {
-            char buf[128];
-            int len = socket.read(buf, sizeof(buf) - 1);
-            buf[len] = '\0';
-            printf("%s", buf);
-        }
-        printf("\nClosing...\n");
-        socket.close();
-    }
-    printf("Done.\n");
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 echoserver.cpp
--- a/echoserver.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void echoserver() {
-    EthernetNetIf eth;
-    eth.setup();
-
-    ServerSocket server(1234);
-
-    while (true) {
-        ClientSocket socket = server.accept();
-        if (socket) {
-            while (socket) {
-                char buf[80];
-                int len = socket.read(buf, sizeof(buf));
-                if (len > 0)
-                    socket.write(buf, len);
-            }
-            socket.close();
-        }
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/echoclient.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/echoclient.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,41 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void echoclient() {
+    EthernetNetIf eth;
+    eth.setup();
+
+    printf("Echo server => ");
+    char server[32];
+    scanf("%s", server);
+
+    ClientSocket socket(server, 1234);
+
+    if (socket) {
+        char message[80] = {};
+        printf("Enter message => ");
+        int c = 0;
+        while (c < ' ' || 0x7E < c)
+            c = getc(stdin);
+        ungetc(c, stdin);
+        for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++)
+            message[i] = c;
+
+        socket.printf("%s", message);
+
+        // wait until data is received
+        while (!socket.available())
+            ;
+        printf("Received: ");
+
+        while (socket.available()) {
+            char buf[128];
+            int len = socket.read(buf, sizeof(buf) - 1);
+            buf[len] = '\0';
+            printf("%s", buf);
+        }
+        printf("\nClosing...\n");
+        socket.close();
+    }
+    printf("Done.\n");
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/echoserver.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/echoserver.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,22 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void echoserver() {
+    EthernetNetIf eth;
+    eth.setup();
+
+    ServerSocket server(1234);
+
+    while (true) {
+        ClientSocket socket = server.accept();
+        if (socket) {
+            while (socket) {
+                char buf[80];
+                int len = socket.read(buf, sizeof(buf));
+                if (len > 0)
+                    socket.write(buf, len);
+            }
+            socket.close();
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/findbuddy.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/findbuddy.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,38 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void findbuddy() {
+    EthernetNetIf eth;
+    eth.setup();
+   
+    IpAddr myIP = eth.getIp();
+    DatagramSocket datagram(myIP, 7777);
+
+    Host buddy;
+    for (int i = myIP[3] + 1;; i = (i - myIP[3] + 260) % 10 + myIP[3] + 1) {
+        printf("sending message to %d.%d.%d.%d\n", myIP[0], myIP[1], myIP[2], i);
+        datagram.printf("Hello World\n");
+        datagram.send(IpAddr(myIP[0], myIP[1], myIP[2], i), 7777);
+        // this will not reach target during the first round,
+        // and in order to receive successfully,
+        // timeout must be at least 4 seconds (default = 5.0 seconds)
+        if (datagram.receive(&buddy, 4) > 0) {
+            IpAddr ip = buddy.getIp();
+            char buf[80] = {};
+            int len = datagram.read(buf, sizeof(buf) - 1);
+            printf("received from %d.%d.%d.%d: %s", ip[0],ip[1], ip[2], ip[3], buf);
+            break;
+        }
+    }
+
+    for (int i = 0; i < 2; i++) {
+        datagram.printf("There you are!\n");
+        datagram.send(buddy.getIp(), 7777);
+
+        if (datagram.receive() > 0) {
+            char buf[80] = {};
+            int len = datagram.read(buf, sizeof(buf) - 1);
+            printf("received: %s", buf);
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/httpclient.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/httpclient.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,30 @@
+#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);
+            }
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/multicast.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/multicast.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,26 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void multicast() {
+    EthernetNetIf eth;
+    eth.setup();
+    
+    Host multicast(IpAddr(239, 192, 1, 100), 50000);
+    DatagramSocket datagram(multicast);
+    
+    while (true) {
+        Host host;
+        if (datagram.receive(&host, 1 + (rand() % 5) / 3.0) > 0) {
+            int value;
+            datagram.scanf("%d", &value);
+            IpAddr ip = host.getIp();
+            printf("received from %d.%d.%d.%d:%d %d\n", ip[0], ip[1], ip[2], ip[3], host.getPort(), value);
+        } else {
+            char* message = "12345!";
+            datagram.printf(message);
+            datagram.send(multicast);
+            printf("sent: %s\n", message);
+            wait(1);
+        }
+    }
+}
diff -r 108499af2b53 -r 84182fc63956 examples/ntpclient.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/ntpclient.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,34 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void ntpclient() {
+    EthernetNetIf eth;
+    eth.setup();
+    
+    char *NTP_SERVER = "pool.ntp.org";
+    printf("ntp server = %s\n", NTP_SERVER);
+
+    while (true) {
+        DatagramSocket datagram;
+        char buf[48] = {0x23}; // 00100011 LI(0), Version(4), Mode(3: Client)
+        datagram.write(buf, sizeof(buf));
+        datagram.send(NTP_SERVER, 123);
+        
+        if (datagram.receive() > 0) {
+            if (datagram.read(buf, sizeof(buf)) > 0) {
+                unsigned long seconds = 0;
+                for (int i = 40; i <= 43; i++)
+                    seconds = (seconds << 8) | buf[i];
+                set_time(time_t(seconds - 2208988800ULL));
+                char timestamp[16];
+                time_t jstime = time(NULL) + 9 * 3600;
+                strftime(timestamp, sizeof(timestamp), "%m/%d %X", localtime(&jstime));
+                printf("Time: %s\n", timestamp);
+                break;
+            }
+        } else {
+            printf("no answer\n");
+            wait(1.0);
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/ntpclient2.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/ntpclient2.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,94 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+typedef unsigned long long Time64;
+
+Time64 toTime64(char buf[]) {
+    Time64 time64 = 0;
+    for (int i = 0; i < 8; i++) {
+        time64 = (time64 << 8) | buf[i];
+    }
+    return time64;
+}
+
+Time64 toTime64(int usecs) {
+    return Time64(double(usecs) * (1ULL << 32) / 1000000);
+}
+
+void printTime64(char *title, Time64 time64) {
+    unsigned long seconds = (unsigned long) (time64 >> 32);
+    unsigned long subsecs = (unsigned long) (time64 & 0xFFFFFFFFULL);
+    char buf[16];
+
+    time_t jstime = time_t(seconds - 2208988800UL) + 9 * 3600;
+    strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
+    printf("%s: %s", title, buf);
+    sprintf(buf, "%f\n", (double) subsecs / (1ULL << 32));
+    printf("%s", &buf[1]);
+}
+
+void ntpclient2() {
+    EthernetNetIf eth;
+    eth.setup();
+
+    char *NTP_SERVER = "pool.ntp.org";
+    char buf[48] = {0x23};// 00100011 LI(0), Version(4), Mode(3: Client)
+    Timer timer;
+    Time64 adjustedTime = 0;
+
+    for (int count = 0; count < 5; count++) {
+        buf[0] = 0x23;
+        DatagramSocket datagram;
+        datagram.write(buf, sizeof(buf));
+        timer.reset();
+        datagram.send(NTP_SERVER, 123);
+        timer.start();
+        if (datagram.receive() > 0) {
+            int turnaround = timer.read_us();
+            if (datagram.read(buf, sizeof(buf))) {
+                Time64 receivedTime = toTime64(&buf[32]);
+                Time64 transferTime = toTime64(&buf[40]);
+                adjustedTime = toTime64(turnaround / 2) + receivedTime / 2 + transferTime / 2;
+                timer.reset();
+                timer.start();
+                printTime64("transfer", transferTime);
+                printTime64("adjusted", adjustedTime);
+                printf("\n");
+            }
+        } else {
+            wait(5);
+        }
+    }
+
+    float subsecs = (double) (adjustedTime & 0xFFFFFFFFULL) / (1ULL << 32);
+    wait(1 - subsecs);
+    set_time((size_t) ((adjustedTime >> 32) - 2208988800UL) + 1);
+
+    time_t jstime = time(NULL) + 9 * 3600;
+    strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
+    printf("RTC delta = %6d, %s\n\n", timer.read_us(), buf);
+
+    for (int count = 0; count < 20; count++) {
+        buf[0] = 0x23;
+        DatagramSocket datagram;
+        datagram.write(buf, sizeof(buf));
+        timer.reset();
+        datagram.send(NTP_SERVER, 123);
+        timer.start();
+        if (datagram.receive() > 0) {
+            int turnaround = timer.read_us();
+            if (datagram.read(buf, sizeof(buf))) {
+                Time64 receivedTime = toTime64(&buf[32]);
+                Time64 transferTime = toTime64(&buf[40]);
+                adjustedTime = toTime64(turnaround / 2) + receivedTime / 2 + transferTime / 2;  
+                printTime64("adjusted", adjustedTime);
+                time_t jstime = time(NULL) + 9 * 3600;
+                strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
+                printf("     RTC: %s\n\n", buf);
+            }
+        } else {
+            wait(5);
+        }
+    }
+
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/supertweet.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/supertweet.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,87 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+#include <ctype.h>
+
+void encodeBase64(char *ibuf, char *obuf);
+int encodeFormUrl(char *s, char *t);
+
+void supertweet() {
+    EthernetNetIf eth;
+    eth.setup();
+    ClientSocket client("66.180.175.246", 80); // api.supertweet.net
+
+    char user[16], password[16], message[64] = {};
+    printf("user => ");
+    scanf("%s", user);
+    printf("password => ");
+    scanf("%s", password);
+    printf("message => ");
+    int c = 0;
+    while (c < ' ' || 0x7E < c)
+        c = getc(stdin);
+    ungetc(c, stdin);
+    for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++)
+        message[i] = c;
+
+    char credential[48], credential2[64], message2[256];
+
+    sprintf(credential, "%s:%s", user, password);
+    encodeBase64(credential, credential2);
+    encodeFormUrl(message, message2);
+
+    const char *request =
+        "POST /1/statuses/update.xml HTTP/1.1\r\n"
+        "Host: api.supertweet.net\r\n"
+        "Authorization: Basic %s\r\n"
+        "Content-Length: %d\r\n"
+        "Content-Type: application/x-www-form-urlencoded\r\n"
+        "\r\n"
+        "status=%s";
+
+    client.printf(request, credential2, strlen(message2) + 7,  message2);
+    printf(request, credential2, strlen(message2) + 7,  message2);
+    printf("\n");
+
+    while (client) {
+        if (client.available()) {
+            while (client.available()) {
+                char response[128] = {};
+                client.read(response, sizeof(response) - 1);
+                printf("%s", response);
+            }
+            client.close();
+        }
+    }
+    printf("\ndone\n");
+}
+
+int encodeFormUrl(char *s, char *t) {
+    char *head = t;
+    for (char c; (c = *s) != 0; s++)
+        switch (c) {
+            case '\r':
+                break;
+            case ' ' :
+                *t++ = '+';
+                break;
+            default:
+                t += sprintf(t, isalnum(c) ? "%c" : (c == '\n') ? "\r%c" : "%%%02X", c);
+        }
+    *t = '\0';
+    return t - head;
+}
+
+void encodeBase64(char ibuf[], int length, char *obuf) {
+    const char BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+    int i, j;
+    for (i = j = 0; j < length; j += 3, i += 4) {
+        long a = ibuf[j] << 16 | (j + 1 < length ? ibuf[j + 1] << 8 : 0) | (j + 2 < length ? ibuf[j + 2] : 0);
+        for (int k = 3; k >= 0; k--, a >>= 6)
+            obuf[i + k] = (j + k - 1) < length ? BASE64[a & 63] : '=';
+    }
+    obuf[i] = '\0';
+}
+
+void encodeBase64(char *ibuf, char *obuf) {
+    encodeBase64(ibuf, strlen(ibuf), obuf);
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/udpreceiver.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/udpreceiver.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,20 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void udpreceiver() {
+    EthernetNetIf eth;
+    eth.setup();
+    
+    DatagramSocket datagram(7777);
+
+    Host buddy;
+    while (true) {
+        if (datagram.receive(&buddy, 1) > 0) {
+            IpAddr ip = buddy.getIp();
+            int port = buddy.getPort();
+            char buf[80] = {};
+            int len = datagram.read(buf, sizeof(buf) - 1);
+            printf("received from %d.%d.%d.%d:%d %s", ip[0],ip[1], ip[2], ip[3], port, buf);
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/udpsender.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/udpsender.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,29 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void udpsender() {
+    EthernetNetIf eth;
+    eth.setup();
+    
+    DatagramSocket datagram;
+
+    char message[80] = {};
+    printf("Enter message => ");
+    int c = 0;
+    while (c < ' ' || 0x7E < c)
+        c = getc(stdin);
+    ungetc(c, stdin);
+    for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++)
+        message[i] = c;
+
+    int i1, i2, i3, i4;
+    printf("UDP receiver address => ");
+    scanf("%d.%d.%d.%d", &i1, &i2, &i3, &i4);
+
+    for (int i = 0;; i++) {
+        printf("Sending message : %s (%d)\n", message, i);
+        datagram.printf("(%d) %s\n", i, message);
+        datagram.send(IpAddr(i1, i2, i3, i4), 7777);
+        wait(1);
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/webcontroller.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/webcontroller.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,50 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void webcontroller() {
+    const char *response0 =
+        "HTTP/1.1 200 OK\r\n"
+        "Content-Type: text/html\r\n"
+        "\r\n"
+        "<html>\n"
+        "<head><title>mbed LED1 Controller</title></head>\n"
+        "<body>\n"
+        "<h4>LED1 Status & Change</h4>\n";
+
+    const char *response1 =
+        "<form method=\"GET\" action=\"/\">\n"
+        "<input type=\"radio\" name=\"LED\" value=\"1\" %s onclick=\"submit();\"/>ON\n"
+        "<input type=\"radio\" name=\"LED\" value=\"0\" %s onclick=\"submit();\"/>OFF\n"
+        "</form>\n";
+    const char *response2 =
+        "</body>\n"
+        "</html>\n";
+
+    DigitalOut led1(LED1);
+    EthernetNetIf eth;
+    eth.setup();
+
+    ServerSocket server(80);
+
+    while (true) {
+        ClientSocket socket = server.accept();
+        while (socket) {
+            if (socket.available()) {
+                char buf[512] = {};
+                socket.read(buf, sizeof(buf) - 1);
+                printf("\n%s\n", buf);
+                led1 = strncmp("GET /?LED=1", buf, 11) == 0;
+
+                printf("LED1 = %d\n\n", led1.read());
+                printf(response0);
+                printf(response1, led1 ? "checked" : "", led1 ? "" : "checked");
+                printf(response2);
+
+                socket.printf(response0);
+                socket.printf(response1, led1 ? "checked" : "", led1 ? "" : "checked");
+                socket.printf(response2);
+                socket.close();
+            }
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 examples/webserver.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/webserver.cpp	Mon Feb 04 09:04:25 2013 +0000
@@ -0,0 +1,51 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void webserver() {
+    const char *response0 =
+        "HTTP/1.1 200 OK\r\n"
+        "Content-Type: text/html\r\n"
+        "\r\n"
+        "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5\">\r\n"
+        "<html>\r\n"
+        "<head>\r\n"
+        "<title>mbed web server</title>\r\n"
+        "</head>\r\n"
+        "<body>\r\n"
+        "<h2>Analog Input</h2>\r\n"
+        "<table cellpadding=\"5\">\r\n";
+    const char *response1 =
+        "<tr style=\"background:#ccccff\">"
+        "<th>pin</th><th>value</th>"
+        "</tr>\r\n";
+    const char *response2 =
+        "<tr style=\"background:#cccccc\">"
+        "<td>p%d</td><td align=\"center\">%f</td>"
+        "</tr>\r\n";
+    const char *response3 =
+        "</table>\r\n"
+        "</body>\r\n"
+        "</html>\r\n";
+
+    EthernetNetIf eth;
+    eth.setup();
+
+    ServerSocket server(80);
+
+    while (true) {
+        ClientSocket socket = server.accept();
+        while (socket) {
+            if (socket.available()) {
+                while (socket.available())
+                    socket.read();
+                socket.printf(response0);
+                socket.printf(response1);
+                AnalogIn analogPin[] = {p15, p16, p17, p18, p19, p20};
+                for (int i = 0; i < 6; i++)
+                    socket.printf(response2, 15 + i, analogPin[i].read());
+                socket.printf(response3);
+                socket.close();
+            }
+        }
+    }
+}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 findbuddy.cpp
--- a/findbuddy.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void findbuddy() {
-    EthernetNetIf eth;
-    eth.setup();
-   
-    IpAddr myIP = eth.getIp();
-    DatagramSocket datagram(myIP, 7777);
-
-    Host buddy;
-    for (int i = myIP[3] + 1;; i = (i - myIP[3] + 260) % 10 + myIP[3] + 1) {
-        printf("sending message to %d.%d.%d.%d\n", myIP[0], myIP[1], myIP[2], i);
-        datagram.printf("Hello World\n");
-        datagram.send(IpAddr(myIP[0], myIP[1], myIP[2], i), 7777);
-        // this will not reach target during the first round,
-        // and in order to receive successfully,
-        // timeout must be at least 4 seconds (default = 5.0 seconds)
-        if (datagram.receive(&buddy, 4) > 0) {
-            IpAddr ip = buddy.getIp();
-            char buf[80] = {};
-            int len = datagram.read(buf, sizeof(buf) - 1);
-            printf("received from %d.%d.%d.%d: %s", ip[0],ip[1], ip[2], ip[3], buf);
-            break;
-        }
-    }
-
-    for (int i = 0; i < 2; i++) {
-        datagram.printf("There you are!\n");
-        datagram.send(buddy.getIp(), 7777);
-
-        if (datagram.receive() > 0) {
-            char buf[80] = {};
-            int len = datagram.read(buf, sizeof(buf) - 1);
-            printf("received: %s", buf);
-        }
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 httpclient.cpp
--- a/httpclient.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-#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);
-            }
-        }
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 multicast.cpp
--- a/multicast.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void multicast() {
-    EthernetNetIf eth;
-    eth.setup();
-    
-    Host multicast(IpAddr(239, 192, 1, 100), 50000);
-    DatagramSocket datagram(multicast);
-    
-    while (true) {
-        Host host;
-        if (datagram.receive(&host, 1 + (rand() % 5) / 3.0) > 0) {
-            int value;
-            datagram.scanf("%d", &value);
-            IpAddr ip = host.getIp();
-            printf("received from %d.%d.%d.%d:%d %d\n", ip[0], ip[1], ip[2], ip[3], host.getPort(), value);
-        } else {
-            char* message = "12345!";
-            datagram.printf(message);
-            datagram.send(multicast);
-            printf("sent: %s\n", message);
-            wait(1);
-        }
-    }
-}
diff -r 108499af2b53 -r 84182fc63956 ntpclient.cpp
--- a/ntpclient.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void ntpclient() {
-    EthernetNetIf eth;
-    eth.setup();
-    
-    char *NTP_SERVER = "pool.ntp.org";
-    printf("ntp server = %s\n", NTP_SERVER);
-
-    while (true) {
-        DatagramSocket datagram;
-        char buf[48] = {0x23}; // 00100011 LI(0), Version(4), Mode(3: Client)
-        datagram.write(buf, sizeof(buf));
-        datagram.send(NTP_SERVER, 123);
-        
-        if (datagram.receive() > 0) {
-            if (datagram.read(buf, sizeof(buf)) > 0) {
-                unsigned long seconds = 0;
-                for (int i = 40; i <= 43; i++)
-                    seconds = (seconds << 8) | buf[i];
-                set_time(time_t(seconds - 2208988800ULL));
-                char timestamp[16];
-                time_t jstime = time(NULL) + 9 * 3600;
-                strftime(timestamp, sizeof(timestamp), "%m/%d %X", localtime(&jstime));
-                printf("Time: %s\n", timestamp);
-                break;
-            }
-        } else {
-            printf("no answer\n");
-            wait(1.0);
-        }
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 ntpclient2.cpp
--- a/ntpclient2.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-typedef unsigned long long Time64;
-
-Time64 toTime64(char buf[]) {
-    Time64 time64 = 0;
-    for (int i = 0; i < 8; i++) {
-        time64 = (time64 << 8) | buf[i];
-    }
-    return time64;
-}
-
-Time64 toTime64(int usecs) {
-    return Time64(double(usecs) * (1ULL << 32) / 1000000);
-}
-
-void printTime64(char *title, Time64 time64) {
-    unsigned long seconds = (unsigned long) (time64 >> 32);
-    unsigned long subsecs = (unsigned long) (time64 & 0xFFFFFFFFULL);
-    char buf[16];
-
-    time_t jstime = time_t(seconds - 2208988800UL) + 9 * 3600;
-    strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
-    printf("%s: %s", title, buf);
-    sprintf(buf, "%f\n", (double) subsecs / (1ULL << 32));
-    printf("%s", &buf[1]);
-}
-
-void ntpclient2() {
-    EthernetNetIf eth;
-    eth.setup();
-
-    char *NTP_SERVER = "pool.ntp.org";
-    char buf[48] = {0x23};// 00100011 LI(0), Version(4), Mode(3: Client)
-    Timer timer;
-    Time64 adjustedTime = 0;
-
-    for (int count = 0; count < 5; count++) {
-        buf[0] = 0x23;
-        DatagramSocket datagram;
-        datagram.write(buf, sizeof(buf));
-        timer.reset();
-        datagram.send(NTP_SERVER, 123);
-        timer.start();
-        if (datagram.receive() > 0) {
-            int turnaround = timer.read_us();
-            if (datagram.read(buf, sizeof(buf))) {
-                Time64 receivedTime = toTime64(&buf[32]);
-                Time64 transferTime = toTime64(&buf[40]);
-                adjustedTime = toTime64(turnaround / 2) + receivedTime / 2 + transferTime / 2;
-                timer.reset();
-                timer.start();
-                printTime64("transfer", transferTime);
-                printTime64("adjusted", adjustedTime);
-                printf("\n");
-            }
-        } else {
-            wait(5);
-        }
-    }
-
-    float subsecs = (double) (adjustedTime & 0xFFFFFFFFULL) / (1ULL << 32);
-    wait(1 - subsecs);
-    set_time((size_t) ((adjustedTime >> 32) - 2208988800UL) + 1);
-
-    time_t jstime = time(NULL) + 9 * 3600;
-    strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
-    printf("RTC delta = %6d, %s\n\n", timer.read_us(), buf);
-
-    for (int count = 0; count < 20; count++) {
-        buf[0] = 0x23;
-        DatagramSocket datagram;
-        datagram.write(buf, sizeof(buf));
-        timer.reset();
-        datagram.send(NTP_SERVER, 123);
-        timer.start();
-        if (datagram.receive() > 0) {
-            int turnaround = timer.read_us();
-            if (datagram.read(buf, sizeof(buf))) {
-                Time64 receivedTime = toTime64(&buf[32]);
-                Time64 transferTime = toTime64(&buf[40]);
-                adjustedTime = toTime64(turnaround / 2) + receivedTime / 2 + transferTime / 2;  
-                printTime64("adjusted", adjustedTime);
-                time_t jstime = time(NULL) + 9 * 3600;
-                strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
-                printf("     RTC: %s\n\n", buf);
-            }
-        } else {
-            wait(5);
-        }
-    }
-
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 supertweet.cpp
--- a/supertweet.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-#include <ctype.h>
-
-void encodeBase64(char *ibuf, char *obuf);
-int encodeFormUrl(char *s, char *t);
-
-void supertweet() {
-    EthernetNetIf eth;
-    eth.setup();
-    ClientSocket client("66.180.175.246", 80); // api.supertweet.net
-
-    char user[16], password[16], message[64] = {};
-    printf("user => ");
-    scanf("%s", user);
-    printf("password => ");
-    scanf("%s", password);
-    printf("message => ");
-    int c = 0;
-    while (c < ' ' || 0x7E < c)
-        c = getc(stdin);
-    ungetc(c, stdin);
-    for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++)
-        message[i] = c;
-
-    char credential[48], credential2[64], message2[256];
-
-    sprintf(credential, "%s:%s", user, password);
-    encodeBase64(credential, credential2);
-    encodeFormUrl(message, message2);
-
-    const char *request =
-        "POST /1/statuses/update.xml HTTP/1.1\r\n"
-        "Host: api.supertweet.net\r\n"
-        "Authorization: Basic %s\r\n"
-        "Content-Length: %d\r\n"
-        "Content-Type: application/x-www-form-urlencoded\r\n"
-        "\r\n"
-        "status=%s";
-
-    client.printf(request, credential2, strlen(message2) + 7,  message2);
-    printf(request, credential2, strlen(message2) + 7,  message2);
-    printf("\n");
-
-    while (client) {
-        if (client.available()) {
-            while (client.available()) {
-                char response[128] = {};
-                client.read(response, sizeof(response) - 1);
-                printf("%s", response);
-            }
-            client.close();
-        }
-    }
-    printf("\ndone\n");
-}
-
-int encodeFormUrl(char *s, char *t) {
-    char *head = t;
-    for (char c; (c = *s) != 0; s++)
-        switch (c) {
-            case '\r':
-                break;
-            case ' ' :
-                *t++ = '+';
-                break;
-            default:
-                t += sprintf(t, isalnum(c) ? "%c" : (c == '\n') ? "\r%c" : "%%%02X", c);
-        }
-    *t = '\0';
-    return t - head;
-}
-
-void encodeBase64(char ibuf[], int length, char *obuf) {
-    const char BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-    int i, j;
-    for (i = j = 0; j < length; j += 3, i += 4) {
-        long a = ibuf[j] << 16 | (j + 1 < length ? ibuf[j + 1] << 8 : 0) | (j + 2 < length ? ibuf[j + 2] : 0);
-        for (int k = 3; k >= 0; k--, a >>= 6)
-            obuf[i + k] = (j + k - 1) < length ? BASE64[a & 63] : '=';
-    }
-    obuf[i] = '\0';
-}
-
-void encodeBase64(char *ibuf, char *obuf) {
-    encodeBase64(ibuf, strlen(ibuf), obuf);
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 udpreceiver.cpp
--- a/udpreceiver.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void udpreceiver() {
-    EthernetNetIf eth;
-    eth.setup();
-    
-    DatagramSocket datagram(7777);
-
-    Host buddy;
-    while (true) {
-        if (datagram.receive(&buddy, 1) > 0) {
-            IpAddr ip = buddy.getIp();
-            int port = buddy.getPort();
-            char buf[80] = {};
-            int len = datagram.read(buf, sizeof(buf) - 1);
-            printf("received from %d.%d.%d.%d:%d %s", ip[0],ip[1], ip[2], ip[3], port, buf);
-        }
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 udpsender.cpp
--- a/udpsender.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void udpsender() {
-    EthernetNetIf eth;
-    eth.setup();
-    
-    DatagramSocket datagram;
-
-    char message[80] = {};
-    printf("Enter message => ");
-    int c = 0;
-    while (c < ' ' || 0x7E < c)
-        c = getc(stdin);
-    ungetc(c, stdin);
-    for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++)
-        message[i] = c;
-
-    int i1, i2, i3, i4;
-    printf("UDP receiver address => ");
-    scanf("%d.%d.%d.%d", &i1, &i2, &i3, &i4);
-
-    for (int i = 0;; i++) {
-        printf("Sending message : %s (%d)\n", message, i);
-        datagram.printf("(%d) %s\n", i, message);
-        datagram.send(IpAddr(i1, i2, i3, i4), 7777);
-        wait(1);
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 webcontroller.cpp
--- a/webcontroller.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void webcontroller() {
-    const char *response0 =
-        "HTTP/1.1 200 OK\r\n"
-        "Content-Type: text/html\r\n"
-        "\r\n"
-        "<html>\n"
-        "<head><title>mbed LED1 Controller</title></head>\n"
-        "<body>\n"
-        "<h4>LED1 Status & Change</h4>\n";
-
-    const char *response1 =
-        "<form method=\"GET\" action=\"/\">\n"
-        "<input type=\"radio\" name=\"LED\" value=\"1\" %s onclick=\"submit();\"/>ON\n"
-        "<input type=\"radio\" name=\"LED\" value=\"0\" %s onclick=\"submit();\"/>OFF\n"
-        "</form>\n";
-    const char *response2 =
-        "</body>\n"
-        "</html>\n";
-
-    DigitalOut led1(LED1);
-    EthernetNetIf eth;
-    eth.setup();
-
-    ServerSocket server(80);
-
-    while (true) {
-        ClientSocket socket = server.accept();
-        while (socket) {
-            if (socket.available()) {
-                char buf[512] = {};
-                socket.read(buf, sizeof(buf) - 1);
-                printf("\n%s\n", buf);
-                led1 = strncmp("GET /?LED=1", buf, 11) == 0;
-
-                printf("LED1 = %d\n\n", led1.read());
-                printf(response0);
-                printf(response1, led1 ? "checked" : "", led1 ? "" : "checked");
-                printf(response2);
-
-                socket.printf(response0);
-                socket.printf(response1, led1 ? "checked" : "", led1 ? "" : "checked");
-                socket.printf(response2);
-                socket.close();
-            }
-        }
-    }
-}
\ No newline at end of file
diff -r 108499af2b53 -r 84182fc63956 webserver.cpp
--- a/webserver.cpp	Wed Jun 06 03:34:16 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-#include "EthernetNetIf.h"
-#include "SimpleSocket.h"
-
-void webserver() {
-    const char *response0 =
-        "HTTP/1.1 200 OK\r\n"
-        "Content-Type: text/html\r\n"
-        "\r\n"
-        "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5\">\r\n"
-        "<html>\r\n"
-        "<head>\r\n"
-        "<title>mbed web server</title>\r\n"
-        "</head>\r\n"
-        "<body>\r\n"
-        "<h2>Analog Input</h2>\r\n"
-        "<table cellpadding=\"5\">\r\n";
-    const char *response1 =
-        "<tr style=\"background:#ccccff\">"
-        "<th>pin</th><th>value</th>"
-        "</tr>\r\n";
-    const char *response2 =
-        "<tr style=\"background:#cccccc\">"
-        "<td>p%d</td><td align=\"center\">%f</td>"
-        "</tr>\r\n";
-    const char *response3 =
-        "</table>\r\n"
-        "</body>\r\n"
-        "</html>\r\n";
-
-    EthernetNetIf eth;
-    eth.setup();
-
-    ServerSocket server(80);
-
-    while (true) {
-        ClientSocket socket = server.accept();
-        while (socket) {
-            if (socket.available()) {
-                while (socket.available())
-                    socket.read();
-                socket.printf(response0);
-                socket.printf(response1);
-                AnalogIn analogPin[] = {p15, p16, p17, p18, p19, p20};
-                for (int i = 0; i < 6; i++)
-                    socket.printf(response2, 15 + i, analogPin[i].read());
-                socket.printf(response3);
-                socket.close();
-            }
-        }
-    }
-}
\ No newline at end of file