Robustness test: socket that exchanges data forever between mbed and the host.
Dependencies: PicoTCP lpc1768-picotcp-eth mbed-rtos mbed
main.cpp
00001 #include <mbed.h> 00002 #include <stdarg.h> 00003 #include "EthernetInterface.h" 00004 00005 #define ECHO_SERVER_PORT 7 00006 #define BUFFER_QUANTITY (1024*1024) 00007 #define MAX_NUMBER_OF_RETRIES (10u) 00008 00009 00010 int main() { 00011 00012 printf("Socket exchange data forever...\n"); 00013 00014 EthernetInterface eth; 00015 int connections = 0; 00016 eth.init(); //Use DHCP 00017 eth.connect(); 00018 //printf("IP Address %s\n", eth.getIPAddress()); 00019 00020 TCPSocketServer server; 00021 server.bind(ECHO_SERVER_PORT); 00022 server.listen(); 00023 00024 while (true) { 00025 printf("\nWait for new connection...\n"); 00026 printf("Client number %d\n",++connections); 00027 TCPSocketConnection client; 00028 server.accept(client); 00029 client.set_blocking(false, 2500); // Timeout after (1.5)s 00030 printf("Connection from: %s\n", client.get_address()); 00031 char buffer[1024]; 00032 while (true) { 00033 int retries = 0; 00034 int dataReceived = 0; 00035 int dataSent = 0; 00036 00037 printf("\n\n\nStarting the receiving part...\n"); 00038 while(dataReceived < BUFFER_QUANTITY) 00039 { 00040 int n = client.receive(buffer, sizeof(buffer)); 00041 if (n <= 0) { 00042 printf("Receive error\n"); 00043 retries++; 00044 if(retries >= MAX_NUMBER_OF_RETRIES) 00045 break; 00046 } 00047 dataReceived += n; 00048 } 00049 00050 00051 printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY); 00052 if(dataReceived < BUFFER_QUANTITY) 00053 { 00054 printf("Receiving part of the test has failed. Exiting connection.\n"); 00055 break; 00056 } 00057 else{ 00058 printf("Receiving has passed...\n"); 00059 } 00060 00061 printf("\n\n\nStarting the sending part...\n"); 00062 retries = 0; 00063 while(dataSent < BUFFER_QUANTITY) 00064 { 00065 int n = client.send_all(buffer, sizeof(buffer)); 00066 if (n <= 0) { 00067 printf("Send error\n"); 00068 retries++; 00069 if(retries >= MAX_NUMBER_OF_RETRIES) 00070 break; 00071 } 00072 dataSent += n; 00073 } 00074 00075 printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY); 00076 if(dataSent < BUFFER_QUANTITY) 00077 { 00078 printf("Sending part of the test has failed. Exiting connection.\n"); 00079 break; 00080 } 00081 else 00082 { 00083 printf("Sending test has passed...\n"); 00084 } 00085 00086 00087 printf("\n\n\nStarting echo part...\n"); 00088 dataReceived = dataSent = 0; 00089 00090 while((dataReceived+dataSent) < 2*BUFFER_QUANTITY) 00091 { 00092 int n = client.receive(buffer, sizeof(buffer)); 00093 if (n <= 0) { 00094 printf("Receive error\n"); 00095 } 00096 dataReceived += n; 00097 00098 n = client.send_all(buffer, n); 00099 if (n <= 0) { 00100 printf("Send error, returned 0\n"); 00101 break; 00102 } 00103 dataSent += n; 00104 } 00105 00106 printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY); 00107 if((dataReceived+dataSent) < 2*BUFFER_QUANTITY) 00108 { 00109 printf("Echo test has failed.Exiting connection...\n"); 00110 break; 00111 } 00112 } 00113 00114 client.close(); 00115 printf("Test was finished...\n"); 00116 00117 } 00118 00119 return 0; 00120 }
Generated on Fri Jul 29 2022 08:51:18 by 1.7.2