PicoTCP TCP/UDP parallel traffic.

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "EthernetInterface.h"
00004 
00005 #define ECHO_SERVER_PORT 7
00006 #define UDP_SERVER_PORT  2327
00007 #define BUFFER_QUANTITY   (1024*1024)
00008 #define NUMBER_OF_THREADS   2u
00009 
00010 Queue<void,NUMBER_OF_THREADS> clientList;
00011 
00012 
00013 /****** UDP Connection ******/
00014 void UDPThread(const void * arg);
00015 void startUDPThread(void)
00016 {
00017     Thread * threadUDPEcho = new Thread(UDPThread);
00018 }
00019 
00020 // thread is sending out data on a rate of 1KB at each 10ms
00021 void UDPThread(const void * arg)
00022 {
00023 
00024     UDPSocket server;
00025     Endpoint endp;
00026     char buffer[1024];
00027 
00028     endp.set_address("255.255.255.255",UDP_SERVER_PORT);
00029     printf("Binding result :%d\n", server.bind(UDP_SERVER_PORT));
00030 
00031     server.set_blocking(false,1500);
00032     printf("Started UDP Broadcast server...\n");
00033     server.set_broadcasting();
00034     
00035     while(true)
00036     {
00037             server.sendTo(endp,buffer,sizeof(buffer));
00038             Thread::wait(10);
00039     }
00040     
00041     server.close();
00042 }
00043 
00044 /****** TCP Connection ******/
00045 
00046 void TCPClientThread(const void * arg);
00047 void acceptNewClient(TCPSocketServer & server)
00048 {
00049     TCPSocketConnection *client =new TCPSocketConnection();
00050     server.accept(*client);
00051     client->set_blocking(false, 1500); // Timeout after (1.5)s
00052     printf("Connection from: %s\n", client->get_address());
00053     if(clientList.put((void *)client) != osOK) // queue is full?
00054         delete(client);
00055 }
00056 
00057 void TCPClientThread(const void * arg)
00058 {
00059 
00060     while(true)
00061     {
00062         TCPSocketConnection * client = NULL;
00063         char buffer[1024];
00064         int dataReceived = 0;
00065         int dataSent = 0;
00066         
00067         // client stream
00068         osEvent evt = clientList.get();
00069         if(evt.status == osEventMessage)
00070             client = (TCPSocketConnection *)evt.value.p;
00071          else 
00072             continue;
00073         
00074         while(true)
00075         {
00076             printf("\n\n\nStarting the receiving part...\n");
00077             while(dataReceived < BUFFER_QUANTITY)
00078             {
00079                 int n = client->receive(buffer, sizeof(buffer));
00080                 if (n <= 0) {
00081                     printf("Receive error\n");
00082                     break;
00083                 }
00084                 dataReceived += n;
00085             }
00086             
00087            
00088             printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY);
00089             if(dataReceived < BUFFER_QUANTITY)
00090             {
00091                 printf("Receiving part of the test has failed. Exiting connection.\n");
00092                 break;
00093             }
00094             else{
00095                 printf("Receiving has passed...\n");
00096             }
00097             
00098             printf("\n\n\nStarting the sending part...\n");
00099             while(dataSent < BUFFER_QUANTITY)
00100             {
00101                 int n = client->send_all(buffer, sizeof(buffer));
00102                 if (n <= 0) {
00103                     printf("Send error : %d\n",n);
00104                     break;
00105                 }
00106                 dataSent += n;
00107             }
00108             
00109             printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY);
00110             if(dataSent < BUFFER_QUANTITY)
00111             {
00112                 printf("Sending part of the test has failed. Exiting connection.\n");
00113                 break;
00114             }
00115             else
00116             {
00117                 printf("Sending test has passed...\n");
00118             }
00119             
00120             
00121             printf("\n\n\nStarting echo part...\n");
00122             dataReceived = dataSent = 0;
00123             while((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
00124             {
00125                 int n = client->receive(buffer, sizeof(buffer));
00126                 if (n <= 0) {
00127                     printf("Receive error\n");
00128                     break;
00129                 }
00130                 dataReceived += n;
00131                 
00132                 n = client->send_all(buffer, n);
00133                 if (n <= 0) {
00134                     printf("Send error\n");
00135                     break;
00136                 }
00137                 dataSent += n;
00138             }
00139             
00140             printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY);
00141             if((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
00142             {
00143                 printf("Echo test has failed.Exiting connection...\n");
00144             }
00145             else
00146             {
00147                 printf("Echo test has passed...\n");
00148             }
00149             
00150             
00151             printf("Test was finished...\n");
00152         }
00153         // droping out client
00154         client->close();
00155         delete(client);
00156         break;
00157      }
00158 }
00159 
00160 int main() 
00161 {
00162     EthernetInterface eth;
00163     eth.init(); //Use DHCP
00164     eth.connect();
00165     printf("IP Address %s\n", eth.getIPAddress());
00166     
00167     // startUDP Thread
00168     startUDPThread();
00169     
00170     
00171     TCPSocketServer server;
00172     server.bind(ECHO_SERVER_PORT);
00173     server.listen();
00174     // start client threads
00175     for(int i=0;i<NUMBER_OF_THREADS;i++)
00176     {
00177         new Thread(TCPClientThread);
00178     }
00179     printf("Started TCP server...\n");
00180 
00181     while (true) 
00182     {
00183         acceptNewClient(server);
00184     }
00185 }