This example test the SSL socket connection ability with google.com@443
Dependencies: NNN50_WIFI_API
Revision 1:4d09372e86ed, committed 2017-09-13
- Comitter:
- tsungta
- Date:
- Wed Sep 13 06:58:45 2017 +0000
- Parent:
- 0:3e902eda4f71
- Child:
- 2:a9c2d2fdd48f
- Commit message:
- Update main.cpp
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Sep 12 02:06:32 2017 +0000 +++ b/main.cpp Wed Sep 13 06:58:45 2017 +0000 @@ -15,8 +15,8 @@ #include "EthernetInterface.h" #include "WIFIDevice.h" -const char* ECHO_SERVER_ADDRESS = "192.168.2.13"; -const int ECHO_SERVER_PORT = 1030; +const char* SERVER_ADDRESS = "google.com"; +const int SERVER_PORT = 443; void scanCallback(tstrM2mWifiscanResult result) { @@ -33,7 +33,7 @@ wifi.apScan(scanCallback); - wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TWCYNPC0209_Mac mini", "mayday55555"); + wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TWCYNPC0209_Mac_mini", "mayday55555"); eth.connect(); @@ -47,32 +47,31 @@ printf("Gateway: %s\n", eth.getGateway()); printf("NetworkMask: %s\n", eth.getNetworkMask()); - UDPSocket sock; - sock.init(); - - Endpoint echo_server; - echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); - - char out_buffer[] = "Hello World"; - printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); - sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); - - char in_buffer[256]; //IMPORTANT, array size MUST >= the actual received data size or set to maximum of 1400 if there is uncertainty - int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); + TCPSocketConnection ssl_socket; + + if (wifi.is_AP_connected() && ssl_socket.connect(SERVER_ADDRESS, SERVER_PORT, true) == 0) { //set true to enable ssl socket connection + printf("Connected to Server \n"); + + ssl_socket.set_blocking(false, 3500); + + // Send message to server + char buf_out[] = "GET / HTTP/1.1\r\n\r\n"; + printf("Sending message to Server : '%s' \n",buf_out); + ssl_socket.send(buf_out, sizeof(buf_out)); + + // Receive message from server + char buf_in[1400]; //set receive buffer size to maximum of 1400 bytes + + int n = ssl_socket.receive(buf_in, 1400); + printf("Received %i bytes from server: \n %s \n", n, buf_in); + } else + printf("Unable to connect to server\n"); - if(n <0) - in_buffer[0] = '\0';//IMPORTANT, in case n = -1 when set_bloacking is timeout, prevent the illegal array in_buffer[-1] access - else - in_buffer[n] = '\0'; - - printf("Received message from server: '%s'\n", in_buffer); - - sock.close(); - - eth.disconnect(); - - wifi.sleep(); - + // Clean up + ssl_socket.close(); + eth.disconnect(); + wifi.sleep(); + while(1) { } }