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:
0:50979ffa39b6
Child:
1:7634d07c4310
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 21 15:33:53 2016 +0000
@@ -0,0 +1,201 @@
+/* =====================================================================
+   Copyright © 2016, Avnet (R)
+
+   Contributors:
+     * James M Flynn, www.em.avnet.com 
+ 
+   Licensed under the Apache License, Version 2.0 (the "License"); 
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, 
+   software distributed under the License is distributed on an 
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+   either express or implied. See the License for the specific 
+   language governing permissions and limitations under the License.
+
+    @file          WNCInterface.cpp
+    @version       1.0
+    @date          Sept 2016
+
+======================================================================== */
+
+#include "mbed.h"
+#include "WNCInterface.h"
+
+#include "Socket/Socket.h"
+#include "Socket/TCPSocketConnection.h"
+#include "Socket/UDPSocket.h"
+
+#define DEBUG
+#define MBED_PLATFORM
+#include "HTTPClient.h"
+
+#define CRLF    "\n\r"
+
+#define ntohl(n)    ((n & 0xff) << 24) |\
+                    ((n & 0xff00) << 8) |\
+                    ((n & 0xff0000UL) >> 8) |\
+                    ((n & 0xff000000UL) >> 24)
+    
+int main() {
+    int ret;
+    WNCInterface wnc;
+    printf("STARTING WNCInterface & Socket Test" CRLF);
+    
+    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 a TCP connection
+//
+    TCPSocketConnection tsock;
+    printf("\n\n\n\n------------------------------------" CRLF "starting TCP Socket Test..." CRLF);
+
+    tsock.connect("mbed.org", 80);
+    printf("Connected!" CRLF );
+    char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
+    ret=tsock.send_all(http_cmd, sizeof(http_cmd)-1);
+    printf("TCP Socket sent %d bytes.\r\n",ret);    
+    char buffer[1500];
+
+    tsock.set_blocking (false, 1000);    
+    while (ret>0) {
+        ret = tsock.receive(buffer, sizeof(buffer)-1);
+        buffer[ret] = '\0';
+        if( ret > 0 )
+            printf("Received %d chars from server:" CRLF "<----->" CRLF "%s" CRLF 
+                   "<----->" CRLF, ret, buffer);
+    }
+    tsock.close();      
+
+//
+//demonstrate a UDP connection
+//
+    printf("\n\n\n\n-------------------------------------" CRLF);
+    printf(        "starting UDP Socket Test..." CRLF);
+
+//#define UDP_URL "utcnist.colorado.edu"
+//#define UDP_PORT 37
+#define UDP_URL "pool.ntp.org"
+#define UDP_PORT 123
+
+    UDPSocket usock;
+    usock.init();
+    Endpoint nist;
+
+    nist.set_address(UDP_URL, UDP_PORT);
+    printf("Endpoing set to '%s'/%s/%d, try sending data." CRLF,
+        UDP_URL, nist.get_address(), nist.get_port());
+    
+//    char out_buffer[] = "plop"; // Does not matter
+
+  char out_buffer[48];
+    out_buffer[0] = 0xE3;         // LI, Version, Mode
+    out_buffer[1] = 0;            // Stratum, or type of clock
+    out_buffer[2] = 6;            // Polling Interval
+    out_buffer[3] = 0xEC;         // Peer Clock Precision
+    // 8 bytes of zero for Root Delay & Root Dispersion
+    out_buffer[12]  = 49;
+    out_buffer[13]  = 0x4E;
+    out_buffer[14]  = 49;
+    out_buffer[15]  = 52;
+
+    ret = usock.sendTo(nist, out_buffer, sizeof(out_buffer));
+    printf("sent buffer to UDP IP/Port (ret=%d)" CRLF "Now try receiving." CRLF,ret);
+
+
+    usock.set_blocking (true, 1000);    
+    char in_buffer[50];
+    int n = usock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
+    
+    printf("Received %d bytes via UDP IP address %s on port %d." CRLF, 
+            n, nist.get_address(), nist.get_port());
+    
+    usock.close();
+
+//
+//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("http://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());
+    }
+    
+    //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) {}
+}
+