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.

Revision:
9:667c078c40c6
Parent:
7:beefa072a165
Child:
10:51937195d6d1
--- a/main.cpp	Wed Nov 23 01:14:11 2016 +0000
+++ b/main.cpp	Fri Dec 02 16:53:32 2016 +0000
@@ -1,5 +1,6 @@
 
 #include "mbed.h"
+ 
 #include "WNCInterface.h"
 
 #include "Socket/Socket.h"
@@ -10,100 +11,234 @@
 #define MBED_PLATFORM
 #include "HTTPClient.h"
 
+#define STR_SIZE    1024
+#define STREAM_CNT  5
+
 #define CRLF    "\n\r"
 
+MODSERIAL pc(USBTX,USBRX,256,256);
+
+void https_test_thread(void);
+
 int main() {
+    Thread http_test(osPriorityNormal, DEFAULT_STACK_SIZE*4, NULL);
+    http_test.start(https_test_thread);
+    while (true) {
+        osDelay(500);
+    }
+}
+
+void test_http(void);
+void test_https(void);
+        
+void https_test_thread(void) {
     int ret;
+
     WNCInterface wnc;
-    MODSERIAL pc(USBTX,USBRX,256,256);
     
-    pc.baud(115200);
-    pc.printf("STARTING WNCInterface & Socket Test" CRLF);
+    //pc.baud(9600);
+    printf("STARTING WNCInterface & Socket Test" CRLF);
     
-    ret = wnc.init();                     
-    pc.printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
+    ret = wnc.init();  
+                       
+    printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
     if( !ret ) {
         printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
         while(1);
-    }
+        }
         
     ret = wnc.connect();                 
     printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress());
 
-//
-//demonstrate HTTPClient operations
-//
-    HTTPClient  http;
-    char str[512];
-
-    printf("\n\n\n\n-------------------------------------" CRLF);
-    printf(        "starting HTTPClient Socket Test..." CRLF);
-
-    //GET data
-    printf(">>>>Fetch a page..." CRLF);
-
-    ret = http.get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, strlen(str));
-    if (!ret)
-    {
-      printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
-      printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str); 
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
-    }
-
-    //POST data
-    HTTPMap map;
-    HTTPText inText(str, 512);
-    map.put("Hello", "World");
-    map.put("test", "1234");
-
-    printf(CRLF CRLF ">>>>Post data..." CRLF);
-    ret = http.post("http://httpbin.org/post", map, &inText);
-    if (!ret)
-    {
-      printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
-      printf("<----->" CRLF );
-      printf("Result: %s" CRLF "<----->" CRLF, str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
-    }
+    test_http();
+    test_https();
     
-    //PUT data
-    strcpy(str, "This is a PUT test!");
-    HTTPText outText(str);
-    printf(CRLF CRLF ">>>>Put data..." CRLF);
-    ret = http.put("http://httpbin.org/put", outText, &inText);
-    if (!ret)
-    {
-      printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
-      printf("<----->" CRLF );
-      printf("Result: %s" CRLF "<----->" CRLF, str); 
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
-    }
-    
-    //DELETE data
-    printf(CRLF CRLF ">>>>Delete data..." CRLF);
-    ret = http.del("http://httpbin.org/delete", &inText);
-    if (!ret)
-    {
-      printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
-      printf("<----->" CRLF );
-      printf("Result: %s" CRLF "<----->" CRLF, str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
-    }
-
     wnc.disconnect();
     printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
     while(1) {}
 }
 
+
+
+void test_http(void) {
+//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
+//demonstrate HTTPClient operations
+//
+    printf(CRLF CRLF">>>  Do the HTTPClient Tests  <<< "CRLF);
+    printf(         "-----------------------------------------" CRLF CRLF);
+    HTTPClient  http;
+    char str[STR_SIZE];
+    int ret;
+    
+    //GET data
+    printf(" ** Fetch a page... **" CRLF);
+
+    ret = http.get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, STR_SIZE);
+    if (!ret) {
+      printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str); 
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
+
+    //POST data
+    HTTPMap map;
+    HTTPText inText(str, STR_SIZE);
+
+    map.put("Hello", "World");
+    map.put("test", "1234");
+
+    printf(CRLF CRLF " ** Post data... **" CRLF);
+    ret = http.post("http://httpbin.org/post", map, &inText);
+    if (!ret) {
+      printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str);
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
+    
+    //PUT data
+    
+    strcpy(str, "This is a PUT test!");
+    HTTPText outText(str);    
+    printf(CRLF CRLF " ** Put data... **" CRLF);
+
+    ret = http.put("http://httpbin.org/put", outText, &inText);
+    if (!ret) {
+      printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str); 
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
+    
+    //DELETE data
+    printf(CRLF CRLF " ** Delete data... **" CRLF);
+    ret = http.del("http://httpbin.org/delete", &inText);
+    if (!ret) {
+      printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str);
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
+
+    printf(CRLF CRLF " ** HTTP:stream data " INTSTR(STREAM_CNT) " times... **" CRLF);
+    if( (ret=http.get("http://httpbin.org:80/stream/" INTSTR(STREAM_CNT), str,sizeof(str))) == HTTP_OK) {
+        printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
+        printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF CRLF, str);
+        }
+    else
+        printf(CRLF "STREAM FAILED!, returned %d" CRLF, ret);
+        
+        
+    printf(CRLF CRLF ">>>>HTTP:Status..." CRLF);
+    ret = http.get("http://httpbin.org/get?show_env=1", str, STR_SIZE);
+    if (!ret) {
+      printf("Executed STATUS successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str);
+      }
+    else {
+      printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -http.getHTTPResponseCode());
+      }
+ 
+}
+
+
+void test_https(void) {
+//
+// Now do HTTPS exchange
+//
+    HTTPSClient *https = new HTTPSClient;
+    char str[STR_SIZE];
+    int ret;
+    
+    https->setHost("developer.mbed.org");
+    https->addRootCACertificate(SSL_CA_PEM);
+
+    printf(CRLF CRLF">>>  Do HTTPSClient Tests  <<< "CRLF);
+    printf(         "-------------------------------" CRLF CRLF);
+ 
+    //GET data
+
+    memset(str,0x00,STR_SIZE);
+    printf(CRLF " ** HTTPS:Fetch a page... **" CRLF);
+                                                   
+    ret = https->get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, STR_SIZE);
+    if (!ret){
+      printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str); 
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
+
+    delete https;
+
+    https = new HTTPSClient;
+    https->setHost("httpbin.org");
+    https->addRootCACertificate(SSL_CA_HTTPBIN);
+
+    HTTPMap map;
+    HTTPText inText(str, STR_SIZE);
+
+    //POST data
+    map.put("https Testing", "https doing a Post");
+    printf(CRLF CRLF ">>>>HTTPS:Post data..." CRLF);
+    ret = https->post("https://httpbin.org:443/post", map, &inText);
+    if (!ret) {
+      printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str);
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
+
+    //PUT data
+    memset(str,0x00,STR_SIZE);
+    strcpy(str, "This is an HTTPS PUT test!");
+    HTTPText outText(str);    
+    printf(CRLF CRLF ">>>>HTTPS:Put data..." CRLF);
+    ret = https->put("https://httpbin.org:443/put", outText, &inText);
+    if (!ret) {
+      printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str); 
+      }
+    else 
+      printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
+   
+    printf(CRLF CRLF ">>>>HTTPS:stream data..." CRLF);
+    if( (ret=https->get("https://httpbin.org:443/stream/" INTSTR(STREAM_CNT), str,sizeof(str))) == HTTP_OK) {
+        printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
+        printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF, str);
+        }
+    else
+        printf(CRLF "STREAM FAILED!, returned %d - HTTP return code = -x%04X" CRLF, ret, -https->getHTTPResponseCode());
+    //DELETE data
+    printf(CRLF CRLF ">>>>HTTPS:Delete data..." CRLF);
+    ret = https->del("https://httpbin.org/delete", &inText);
+    if (!ret) {
+      printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str);
+      }
+    else {
+      printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
+      }
+    
+   
+    printf(CRLF CRLF ">>>>HTTPS:Status..." CRLF);
+    ret = https->get("https://httpbin.org:443/get?show_env=1", str, STR_SIZE);
+    if (!ret) {
+      printf("Executed STATUS successfully - read %d characters" CRLF, strlen(str));
+      printf("<----->" CRLF );
+      printf("Result: %s" CRLF "<----->" CRLF, str);
+      }
+    else {
+      printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
+      }
+      
+    delete https;
+}