TCP Echo Server Example using WizFi310

Dependencies:   NetworkSocketAPI WizFi310Interface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* NetworkSocketAPI Example Program
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 #include "mbed.h"
00018 #include "TCPSocket.h"
00019 #include "mbed.h"
00020 #include "WizFi310Interface.h"
00021 #include "TCPSocket.h"
00022 #include "TCPServer.h"
00023 
00024 #define TARGET_NUCLEO_F411RE
00025 
00026 
00027 #if defined(TARGET_NUCLEO_F411RE)
00028 //Serial pc(USBTX, USBRX);
00029 //WizFi310Internet wifi(PA_11, PA_12, D6, D7, D3, NC, 115200);
00030 //WizFi310Interface wifi(D8, D2, D7, D6, D9, NC, 115200);
00031 #endif
00032 
00033 #if defined(TARGET_WIZwiki_W7500)
00034 Serial pc(USBTX, USBRX);
00035 WizFi310Interface wifi(D1, D0, D7, D6, D8, NC, 115200);
00036 #endif
00037 
00038 #define AP_SSID "wizms1"
00039 #define AP_PASSWORD "maker0701"
00040 #define AP_SECURITY NSAPI_SECURITY_WPA2
00041 
00042 int main()
00043 {
00044     pc.baud(115200);
00045     printf("WizFi310 NetworkSocketAPI TCP Server Example\r\n");
00046     
00047     wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY);
00048     
00049     const char *ip = wifi.get_ip_address();
00050     const char *mac = wifi.get_mac_address();
00051     printf("IP address is: %s\r\n", ip ? ip : "No IP");
00052     printf("MAC address is: %s\r\n", mac ? mac : "No MAC");
00053 
00054     TCPServer srv;
00055     TCPSocket clt_sock;
00056     SocketAddress clt_addr;
00057     
00058     srv.open(&wifi);
00059     srv.set_blocking(true);
00060     srv.bind(80);
00061     srv.listen();
00062     srv.accept(&clt_sock);
00063 
00064     char buffer[512];
00065 
00066     while (true)
00067     {
00068         int n = clt_sock.recv(buffer, sizeof(buffer));
00069         if( n < 0 )
00070         {
00071             clt_sock.close();
00072             srv.close();
00073         }
00074         if( n > 0 )
00075         {
00076             buffer[n] = '\0';
00077             printf("length : %d\r\n", n);
00078             clt_sock.send(buffer, n);
00079         }
00080     }
00081 }