This Socket Example Program Implements a TCP Socket, UDP Socket, and HTTPClient socket then uses each of these to perform basic I/O.

Dependencies:   JSON M2XStreamClient-JMF WNCInterface mbed-rtos mbed

See the README for details on this example program. NOTE: When started, the program can take up to 40 seconds before it will respond. This delay is the time required for the WNC Data Module to connect with the network.

Committer:
JMF
Date:
Fri Dec 02 16:53:32 2016 +0000
Revision:
9:667c078c40c6
Parent:
7:beefa072a165
Child:
10:51937195d6d1
Example showing HTTPS usage.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:50979ffa39b6 1
JMF 0:50979ffa39b6 2 #include "mbed.h"
JMF 9:667c078c40c6 3
JMF 0:50979ffa39b6 4 #include "WNCInterface.h"
JMF 0:50979ffa39b6 5
JMF 0:50979ffa39b6 6 #include "Socket/Socket.h"
JMF 0:50979ffa39b6 7 #include "Socket/TCPSocketConnection.h"
JMF 0:50979ffa39b6 8 #include "Socket/UDPSocket.h"
JMF 0:50979ffa39b6 9
JMF 0:50979ffa39b6 10 #define DEBUG
JMF 0:50979ffa39b6 11 #define MBED_PLATFORM
JMF 0:50979ffa39b6 12 #include "HTTPClient.h"
JMF 0:50979ffa39b6 13
JMF 9:667c078c40c6 14 #define STR_SIZE 1024
JMF 9:667c078c40c6 15 #define STREAM_CNT 5
JMF 9:667c078c40c6 16
JMF 0:50979ffa39b6 17 #define CRLF "\n\r"
JMF 0:50979ffa39b6 18
JMF 9:667c078c40c6 19 MODSERIAL pc(USBTX,USBRX,256,256);
JMF 9:667c078c40c6 20
JMF 9:667c078c40c6 21 void https_test_thread(void);
JMF 9:667c078c40c6 22
JMF 0:50979ffa39b6 23 int main() {
JMF 9:667c078c40c6 24 Thread http_test(osPriorityNormal, DEFAULT_STACK_SIZE*4, NULL);
JMF 9:667c078c40c6 25 http_test.start(https_test_thread);
JMF 9:667c078c40c6 26 while (true) {
JMF 9:667c078c40c6 27 osDelay(500);
JMF 9:667c078c40c6 28 }
JMF 9:667c078c40c6 29 }
JMF 9:667c078c40c6 30
JMF 9:667c078c40c6 31 void test_http(void);
JMF 9:667c078c40c6 32 void test_https(void);
JMF 9:667c078c40c6 33
JMF 9:667c078c40c6 34 void https_test_thread(void) {
JMF 0:50979ffa39b6 35 int ret;
JMF 9:667c078c40c6 36
JMF 0:50979ffa39b6 37 WNCInterface wnc;
JMF 2:8fb99c9f5075 38
JMF 9:667c078c40c6 39 //pc.baud(9600);
JMF 9:667c078c40c6 40 printf("STARTING WNCInterface & Socket Test" CRLF);
JMF 0:50979ffa39b6 41
JMF 9:667c078c40c6 42 ret = wnc.init();
JMF 9:667c078c40c6 43
JMF 9:667c078c40c6 44 printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
JMF 0:50979ffa39b6 45 if( !ret ) {
JMF 0:50979ffa39b6 46 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 47 while(1);
JMF 9:667c078c40c6 48 }
JMF 0:50979ffa39b6 49
JMF 0:50979ffa39b6 50 ret = wnc.connect();
JMF 0:50979ffa39b6 51 printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress());
JMF 0:50979ffa39b6 52
JMF 9:667c078c40c6 53 test_http();
JMF 9:667c078c40c6 54 test_https();
JMF 0:50979ffa39b6 55
JMF 0:50979ffa39b6 56 wnc.disconnect();
JMF 0:50979ffa39b6 57 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 58 while(1) {}
JMF 0:50979ffa39b6 59 }
JMF 0:50979ffa39b6 60
JMF 9:667c078c40c6 61
JMF 9:667c078c40c6 62
JMF 9:667c078c40c6 63 void test_http(void) {
JMF 9:667c078c40c6 64 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
JMF 9:667c078c40c6 65 //demonstrate HTTPClient operations
JMF 9:667c078c40c6 66 //
JMF 9:667c078c40c6 67 printf(CRLF CRLF">>> Do the HTTPClient Tests <<< "CRLF);
JMF 9:667c078c40c6 68 printf( "-----------------------------------------" CRLF CRLF);
JMF 9:667c078c40c6 69 HTTPClient http;
JMF 9:667c078c40c6 70 char str[STR_SIZE];
JMF 9:667c078c40c6 71 int ret;
JMF 9:667c078c40c6 72
JMF 9:667c078c40c6 73 //GET data
JMF 9:667c078c40c6 74 printf(" ** Fetch a page... **" CRLF);
JMF 9:667c078c40c6 75
JMF 9:667c078c40c6 76 ret = http.get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, STR_SIZE);
JMF 9:667c078c40c6 77 if (!ret) {
JMF 9:667c078c40c6 78 printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 79 printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 80 }
JMF 9:667c078c40c6 81 else
JMF 9:667c078c40c6 82 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 83
JMF 9:667c078c40c6 84 //POST data
JMF 9:667c078c40c6 85 HTTPMap map;
JMF 9:667c078c40c6 86 HTTPText inText(str, STR_SIZE);
JMF 9:667c078c40c6 87
JMF 9:667c078c40c6 88 map.put("Hello", "World");
JMF 9:667c078c40c6 89 map.put("test", "1234");
JMF 9:667c078c40c6 90
JMF 9:667c078c40c6 91 printf(CRLF CRLF " ** Post data... **" CRLF);
JMF 9:667c078c40c6 92 ret = http.post("http://httpbin.org/post", map, &inText);
JMF 9:667c078c40c6 93 if (!ret) {
JMF 9:667c078c40c6 94 printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 95 printf("<----->" CRLF );
JMF 9:667c078c40c6 96 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 97 }
JMF 9:667c078c40c6 98 else
JMF 9:667c078c40c6 99 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 100
JMF 9:667c078c40c6 101 //PUT data
JMF 9:667c078c40c6 102
JMF 9:667c078c40c6 103 strcpy(str, "This is a PUT test!");
JMF 9:667c078c40c6 104 HTTPText outText(str);
JMF 9:667c078c40c6 105 printf(CRLF CRLF " ** Put data... **" CRLF);
JMF 9:667c078c40c6 106
JMF 9:667c078c40c6 107 ret = http.put("http://httpbin.org/put", outText, &inText);
JMF 9:667c078c40c6 108 if (!ret) {
JMF 9:667c078c40c6 109 printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 110 printf("<----->" CRLF );
JMF 9:667c078c40c6 111 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 112 }
JMF 9:667c078c40c6 113 else
JMF 9:667c078c40c6 114 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 115
JMF 9:667c078c40c6 116 //DELETE data
JMF 9:667c078c40c6 117 printf(CRLF CRLF " ** Delete data... **" CRLF);
JMF 9:667c078c40c6 118 ret = http.del("http://httpbin.org/delete", &inText);
JMF 9:667c078c40c6 119 if (!ret) {
JMF 9:667c078c40c6 120 printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 121 printf("<----->" CRLF );
JMF 9:667c078c40c6 122 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 123 }
JMF 9:667c078c40c6 124 else
JMF 9:667c078c40c6 125 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 126
JMF 9:667c078c40c6 127 printf(CRLF CRLF " ** HTTP:stream data " INTSTR(STREAM_CNT) " times... **" CRLF);
JMF 9:667c078c40c6 128 if( (ret=http.get("http://httpbin.org:80/stream/" INTSTR(STREAM_CNT), str,sizeof(str))) == HTTP_OK) {
JMF 9:667c078c40c6 129 printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 130 printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF CRLF, str);
JMF 9:667c078c40c6 131 }
JMF 9:667c078c40c6 132 else
JMF 9:667c078c40c6 133 printf(CRLF "STREAM FAILED!, returned %d" CRLF, ret);
JMF 9:667c078c40c6 134
JMF 9:667c078c40c6 135
JMF 9:667c078c40c6 136 printf(CRLF CRLF ">>>>HTTP:Status..." CRLF);
JMF 9:667c078c40c6 137 ret = http.get("http://httpbin.org/get?show_env=1", str, STR_SIZE);
JMF 9:667c078c40c6 138 if (!ret) {
JMF 9:667c078c40c6 139 printf("Executed STATUS successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 140 printf("<----->" CRLF );
JMF 9:667c078c40c6 141 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 142 }
JMF 9:667c078c40c6 143 else {
JMF 9:667c078c40c6 144 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -http.getHTTPResponseCode());
JMF 9:667c078c40c6 145 }
JMF 9:667c078c40c6 146
JMF 9:667c078c40c6 147 }
JMF 9:667c078c40c6 148
JMF 9:667c078c40c6 149
JMF 9:667c078c40c6 150 void test_https(void) {
JMF 9:667c078c40c6 151 //
JMF 9:667c078c40c6 152 // Now do HTTPS exchange
JMF 9:667c078c40c6 153 //
JMF 9:667c078c40c6 154 HTTPSClient *https = new HTTPSClient;
JMF 9:667c078c40c6 155 char str[STR_SIZE];
JMF 9:667c078c40c6 156 int ret;
JMF 9:667c078c40c6 157
JMF 9:667c078c40c6 158 https->setHost("developer.mbed.org");
JMF 9:667c078c40c6 159 https->addRootCACertificate(SSL_CA_PEM);
JMF 9:667c078c40c6 160
JMF 9:667c078c40c6 161 printf(CRLF CRLF">>> Do HTTPSClient Tests <<< "CRLF);
JMF 9:667c078c40c6 162 printf( "-------------------------------" CRLF CRLF);
JMF 9:667c078c40c6 163
JMF 9:667c078c40c6 164 //GET data
JMF 9:667c078c40c6 165
JMF 9:667c078c40c6 166 memset(str,0x00,STR_SIZE);
JMF 9:667c078c40c6 167 printf(CRLF " ** HTTPS:Fetch a page... **" CRLF);
JMF 9:667c078c40c6 168
JMF 9:667c078c40c6 169 ret = https->get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, STR_SIZE);
JMF 9:667c078c40c6 170 if (!ret){
JMF 9:667c078c40c6 171 printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 172 printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 173 }
JMF 9:667c078c40c6 174 else
JMF 9:667c078c40c6 175 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 176
JMF 9:667c078c40c6 177 delete https;
JMF 9:667c078c40c6 178
JMF 9:667c078c40c6 179 https = new HTTPSClient;
JMF 9:667c078c40c6 180 https->setHost("httpbin.org");
JMF 9:667c078c40c6 181 https->addRootCACertificate(SSL_CA_HTTPBIN);
JMF 9:667c078c40c6 182
JMF 9:667c078c40c6 183 HTTPMap map;
JMF 9:667c078c40c6 184 HTTPText inText(str, STR_SIZE);
JMF 9:667c078c40c6 185
JMF 9:667c078c40c6 186 //POST data
JMF 9:667c078c40c6 187 map.put("https Testing", "https doing a Post");
JMF 9:667c078c40c6 188 printf(CRLF CRLF ">>>>HTTPS:Post data..." CRLF);
JMF 9:667c078c40c6 189 ret = https->post("https://httpbin.org:443/post", map, &inText);
JMF 9:667c078c40c6 190 if (!ret) {
JMF 9:667c078c40c6 191 printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 192 printf("<----->" CRLF );
JMF 9:667c078c40c6 193 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 194 }
JMF 9:667c078c40c6 195 else
JMF 9:667c078c40c6 196 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 197
JMF 9:667c078c40c6 198 //PUT data
JMF 9:667c078c40c6 199 memset(str,0x00,STR_SIZE);
JMF 9:667c078c40c6 200 strcpy(str, "This is an HTTPS PUT test!");
JMF 9:667c078c40c6 201 HTTPText outText(str);
JMF 9:667c078c40c6 202 printf(CRLF CRLF ">>>>HTTPS:Put data..." CRLF);
JMF 9:667c078c40c6 203 ret = https->put("https://httpbin.org:443/put", outText, &inText);
JMF 9:667c078c40c6 204 if (!ret) {
JMF 9:667c078c40c6 205 printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 206 printf("<----->" CRLF );
JMF 9:667c078c40c6 207 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 208 }
JMF 9:667c078c40c6 209 else
JMF 9:667c078c40c6 210 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 211
JMF 9:667c078c40c6 212 printf(CRLF CRLF ">>>>HTTPS:stream data..." CRLF);
JMF 9:667c078c40c6 213 if( (ret=https->get("https://httpbin.org:443/stream/" INTSTR(STREAM_CNT), str,sizeof(str))) == HTTP_OK) {
JMF 9:667c078c40c6 214 printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 215 printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 216 }
JMF 9:667c078c40c6 217 else
JMF 9:667c078c40c6 218 printf(CRLF "STREAM FAILED!, returned %d - HTTP return code = -x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 219 //DELETE data
JMF 9:667c078c40c6 220 printf(CRLF CRLF ">>>>HTTPS:Delete data..." CRLF);
JMF 9:667c078c40c6 221 ret = https->del("https://httpbin.org/delete", &inText);
JMF 9:667c078c40c6 222 if (!ret) {
JMF 9:667c078c40c6 223 printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 224 printf("<----->" CRLF );
JMF 9:667c078c40c6 225 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 226 }
JMF 9:667c078c40c6 227 else {
JMF 9:667c078c40c6 228 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 229 }
JMF 9:667c078c40c6 230
JMF 9:667c078c40c6 231
JMF 9:667c078c40c6 232 printf(CRLF CRLF ">>>>HTTPS:Status..." CRLF);
JMF 9:667c078c40c6 233 ret = https->get("https://httpbin.org:443/get?show_env=1", str, STR_SIZE);
JMF 9:667c078c40c6 234 if (!ret) {
JMF 9:667c078c40c6 235 printf("Executed STATUS successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 236 printf("<----->" CRLF );
JMF 9:667c078c40c6 237 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 238 }
JMF 9:667c078c40c6 239 else {
JMF 9:667c078c40c6 240 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 241 }
JMF 9:667c078c40c6 242
JMF 9:667c078c40c6 243 delete https;
JMF 9:667c078c40c6 244 }