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.
Diff: main.cpp
- Revision:
- 6:562eed9eca87
- Parent:
- 3:5cc9de44aea5
- Child:
- 7:beefa072a165
--- a/main.cpp Thu Nov 17 18:37:59 2016 +0000 +++ b/main.cpp Tue Nov 22 15:23:34 2016 +0000 @@ -1,26 +1,3 @@ -/* ===================================================================== - 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" @@ -35,11 +12,6 @@ #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; @@ -57,73 +29,6 @@ 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 (false, 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