SimpleSocket 1.0 examples
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
supertweet.cpp@34:a108bcc26b69, 2011-11-04 (annotated)
- Committer:
- yamaguch
- Date:
- Fri Nov 04 06:04:36 2011 +0000
- Revision:
- 34:a108bcc26b69
- Parent:
- 33:39d9cdf99de8
removed Serial mon,;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch |
33:39d9cdf99de8 | 1 | #include "EthernetNetIf.h" |
yamaguch |
32:00114e36de90 | 2 | #include "SimpleSocket.h" |
yamaguch |
32:00114e36de90 | 3 | #include <ctype.h> |
yamaguch |
33:39d9cdf99de8 | 4 | |
yamaguch |
33:39d9cdf99de8 | 5 | void encodeBase64(char *ibuf, char *obuf); |
yamaguch |
33:39d9cdf99de8 | 6 | int encodeFormUrl(char *s, char *t); |
yamaguch |
33:39d9cdf99de8 | 7 | |
yamaguch |
33:39d9cdf99de8 | 8 | void supertweet() { |
yamaguch |
33:39d9cdf99de8 | 9 | EthernetNetIf eth; |
yamaguch |
33:39d9cdf99de8 | 10 | eth.setup(); |
yamaguch |
33:39d9cdf99de8 | 11 | ClientSocket client("66.180.175.246", 80); // api.supertweet.net |
yamaguch |
33:39d9cdf99de8 | 12 | |
yamaguch |
33:39d9cdf99de8 | 13 | char user[16], password[16], message[64] = {}; |
yamaguch |
33:39d9cdf99de8 | 14 | printf("user => "); |
yamaguch |
33:39d9cdf99de8 | 15 | scanf("%s", user); |
yamaguch |
33:39d9cdf99de8 | 16 | printf("password => "); |
yamaguch |
33:39d9cdf99de8 | 17 | scanf("%s", password); |
yamaguch |
33:39d9cdf99de8 | 18 | printf("message => "); |
yamaguch |
33:39d9cdf99de8 | 19 | int c = 0; |
yamaguch |
33:39d9cdf99de8 | 20 | while (c < ' ' || 0x7E < c) |
yamaguch |
33:39d9cdf99de8 | 21 | c = getc(stdin); |
yamaguch |
33:39d9cdf99de8 | 22 | ungetc(c, stdin); |
yamaguch |
33:39d9cdf99de8 | 23 | for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++) |
yamaguch |
33:39d9cdf99de8 | 24 | message[i] = c; |
yamaguch |
33:39d9cdf99de8 | 25 | |
yamaguch |
33:39d9cdf99de8 | 26 | char credential[48], credential2[64], message2[256]; |
yamaguch |
33:39d9cdf99de8 | 27 | |
yamaguch |
33:39d9cdf99de8 | 28 | sprintf(credential, "%s:%s", user, password); |
yamaguch |
33:39d9cdf99de8 | 29 | encodeBase64(credential, credential2); |
yamaguch |
33:39d9cdf99de8 | 30 | encodeFormUrl(message, message2); |
yamaguch |
33:39d9cdf99de8 | 31 | |
yamaguch |
33:39d9cdf99de8 | 32 | const char *request = |
yamaguch |
33:39d9cdf99de8 | 33 | "POST /1/statuses/update.xml HTTP/1.1\r\n" |
yamaguch |
33:39d9cdf99de8 | 34 | "Host: api.supertweet.net\r\n" |
yamaguch |
33:39d9cdf99de8 | 35 | "Authorization: Basic %s\r\n" |
yamaguch |
33:39d9cdf99de8 | 36 | "Content-Length: %d\r\n" |
yamaguch |
33:39d9cdf99de8 | 37 | "Content-Type: application/x-www-form-urlencoded\r\n" |
yamaguch |
33:39d9cdf99de8 | 38 | "\r\n" |
yamaguch |
33:39d9cdf99de8 | 39 | "status=%s"; |
yamaguch |
33:39d9cdf99de8 | 40 | |
yamaguch |
33:39d9cdf99de8 | 41 | client.printf(request, credential2, strlen(message2) + 7, message2); |
yamaguch |
33:39d9cdf99de8 | 42 | printf(request, credential2, strlen(message2) + 7, message2); |
yamaguch |
33:39d9cdf99de8 | 43 | printf("\n"); |
yamaguch |
33:39d9cdf99de8 | 44 | |
yamaguch |
33:39d9cdf99de8 | 45 | while (client) { |
yamaguch |
33:39d9cdf99de8 | 46 | if (client.available()) { |
yamaguch |
33:39d9cdf99de8 | 47 | while (client.available()) { |
yamaguch |
34:a108bcc26b69 | 48 | char response[128] = {}; |
yamaguch |
33:39d9cdf99de8 | 49 | client.read(response, sizeof(response) - 1); |
yamaguch |
34:a108bcc26b69 | 50 | printf("%s", response); |
yamaguch |
33:39d9cdf99de8 | 51 | } |
yamaguch |
33:39d9cdf99de8 | 52 | client.close(); |
yamaguch |
33:39d9cdf99de8 | 53 | } |
yamaguch |
33:39d9cdf99de8 | 54 | } |
yamaguch |
34:a108bcc26b69 | 55 | printf("\ndone\n"); |
yamaguch |
33:39d9cdf99de8 | 56 | } |
yamaguch |
32:00114e36de90 | 57 | |
yamaguch |
32:00114e36de90 | 58 | int encodeFormUrl(char *s, char *t) { |
yamaguch |
32:00114e36de90 | 59 | char *head = t; |
yamaguch |
32:00114e36de90 | 60 | for (char c; (c = *s) != 0; s++) |
yamaguch |
32:00114e36de90 | 61 | switch (c) { |
yamaguch |
32:00114e36de90 | 62 | case '\r': |
yamaguch |
32:00114e36de90 | 63 | break; |
yamaguch |
32:00114e36de90 | 64 | case ' ' : |
yamaguch |
32:00114e36de90 | 65 | *t++ = '+'; |
yamaguch |
32:00114e36de90 | 66 | break; |
yamaguch |
32:00114e36de90 | 67 | default: |
yamaguch |
32:00114e36de90 | 68 | t += sprintf(t, isalnum(c) ? "%c" : (c == '\n') ? "\r%c" : "%%%02X", c); |
yamaguch |
32:00114e36de90 | 69 | } |
yamaguch |
32:00114e36de90 | 70 | *t = '\0'; |
yamaguch |
32:00114e36de90 | 71 | return t - head; |
yamaguch |
32:00114e36de90 | 72 | } |
yamaguch |
32:00114e36de90 | 73 | |
yamaguch |
32:00114e36de90 | 74 | void encodeBase64(char ibuf[], int length, char *obuf) { |
yamaguch |
32:00114e36de90 | 75 | const char BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
yamaguch |
32:00114e36de90 | 76 | int i, j; |
yamaguch |
32:00114e36de90 | 77 | for (i = j = 0; j < length; j += 3, i += 4) { |
yamaguch |
32:00114e36de90 | 78 | long a = ibuf[j] << 16 | (j + 1 < length ? ibuf[j + 1] << 8 : 0) | (j + 2 < length ? ibuf[j + 2] : 0); |
yamaguch |
32:00114e36de90 | 79 | for (int k = 3; k >= 0; k--, a >>= 6) |
yamaguch |
32:00114e36de90 | 80 | obuf[i + k] = (j + k - 1) < length ? BASE64[a & 63] : '='; |
yamaguch |
32:00114e36de90 | 81 | } |
yamaguch |
32:00114e36de90 | 82 | obuf[i] = '\0'; |
yamaguch |
32:00114e36de90 | 83 | } |
yamaguch |
32:00114e36de90 | 84 | |
yamaguch |
32:00114e36de90 | 85 | void encodeBase64(char *ibuf, char *obuf) { |
yamaguch |
32:00114e36de90 | 86 | encodeBase64(ibuf, strlen(ibuf), obuf); |
yamaguch |
32:00114e36de90 | 87 | } |