WizFi250 Tcp Udp Example

Dependencies:   WizFi250Interface mbed

Fork of WizFi250TcpUdpExample by DongEun Koak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  /* Copyright (C) 2014 Wiznet, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 #include <stdio.h>
00021 #include "mbed.h"
00022 #include "WizFi250Interface.h"
00023 
00024 
00025 #define SECURE WizFi250::SEC_AUTO
00026 #define SSID "DIR-815_Wiznet"
00027 #define PASS "12345678"
00028 
00029 #define ECHO_SERVER_ADDRESS "192.168.18.105"
00030 #define ECHO_SERVER_PORT    5000
00031 
00032 #if defined(TARGET_FRDM_KL25Z)
00033     WizFi250Interface wizfi250(PTE0,PTE1,PTD5,PTD0,PTD4,NC,115200);
00034 #elif defined(TARGET_WIZwiki_W7500)
00035     WizFi250Interface wizfi250(D0,D1,D7,D8,PA_12,NC,115200);
00036     Serial pc(USBTX, USBRX);
00037 #endif
00038 
00039 
00040 
00041 void testUdpEchoClient();
00042 void testUdpEchoServer();
00043 void testTcpEchoClient();
00044 void testTcpEchoServer();
00045 
00046 int main()
00047 {
00048     char input;
00049     
00050     pc.baud(115200);
00051     
00052     wizfi250.init();
00053     if ( wizfi250.connect(SECURE, SSID, PASS))      return -1;
00054     printf("IP Address is %s\r\n", wizfi250.getIPAddress());
00055     
00056     
00057     while(1)
00058     {
00059         printf("\r\nInput Test Mode ( 1:)\r\n");
00060         input = pc.getc();
00061     
00062         if(input == 'q')
00063             break;
00064     
00065         switch(input)
00066         {
00067         case '1':
00068             testTcpEchoServer();
00069             break;
00070         case '2':
00071             testTcpEchoClient();
00072             break;
00073         case '3':
00074             testUdpEchoClient();
00075             break;
00076         case '4':
00077             testUdpEchoServer();
00078             break;
00079         default:
00080             break;        
00081         }
00082     }
00083     
00084     printf("End the test program\r\n");
00085 }
00086 
00087 
00088 void testTcpEchoServer()
00089 {
00090     char buffer[1024];
00091     int n = 0;
00092 
00093     TCPSocketServer server;
00094     server.bind(ECHO_SERVER_PORT);
00095     server.listen();
00096 
00097     printf("\nWait for new connection...\r\n");
00098     TCPSocketConnection client;
00099     server.accept(client);
00100     client.set_blocking(false, 1500);
00101 
00102     printf("Connection from: %s\r\n", client.get_address());
00103     while (true)
00104     {
00105         if( client.is_connected() == false )
00106         {
00107             client.close();
00108             server.close();
00109             break;
00110         }
00111         n = client.receive_all(buffer, sizeof(buffer));
00112         if ( n > 0 )
00113         {
00114             buffer[n] = '\0';
00115             printf("length : %d\r\n", n);
00116 
00117             client.send_all(buffer, n);
00118         }
00119     }
00120 }
00121 
00122 void testUdpEchoClient()
00123 {
00124     int i;
00125     UDPSocket sock;
00126     sock.init();
00127 
00128     Endpoint echo_server;
00129     echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00130 
00131 
00132     for(i=0;i<10;i++)
00133     {
00134         char out_buffer[] = "Hello World\n";
00135         sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
00136 
00137         char in_buffer[256];
00138         int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
00139         in_buffer[n] = '\0';
00140 
00141         if( n > 0 )
00142             printf("%s\r\n", in_buffer);
00143     }
00144 
00145     sock.close();
00146 }
00147 
00148 
00149 void testUdpEchoServer()
00150 {
00151         UDPSocket server;
00152         server.set_blocking(false);
00153         server.bind(ECHO_SERVER_PORT);
00154 
00155         Endpoint client;
00156         char buffer[256];
00157         while(true)
00158         {
00159             int n = server.receiveFrom(client, buffer, sizeof(buffer));
00160             if(n > 0)
00161             {
00162                 //INFO("Received packet from: %s\n", client.get_address());
00163                 buffer[n] = '\0';
00164                 printf("%s\r\n", buffer);
00165                 server.sendTo(client, buffer, n);
00166                 break;
00167             }
00168         }
00169 }
00170 
00171 
00172 void testTcpEchoClient()
00173 {
00174     char buffer[512];
00175     TCPSocketConnection socket;
00176 
00177     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0)
00178     {
00179         printf("Unable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00180         wait(1);
00181     }
00182 
00183     while(true)
00184     {
00185         int n = socket.receive(buffer, sizeof(buffer));
00186         if(n > 0)
00187         {
00188             buffer[n] = '\0';
00189             printf("%s\r\n",buffer);
00190             socket.send(buffer, strlen(buffer));
00191             //socket.send_all(buffer, sizeof(buffer)-1);
00192             break;
00193         }
00194     }
00195 
00196     socket.close();
00197 }