Robustness test: socket that exchanges data forever between mbed and the host.

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

Committer:
tass
Date:
Wed Jul 24 13:53:23 2013 +0000
Revision:
0:b88f77e8d572
Changed the memory test so it would exchange data forever,; if no failure appears.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tass 0:b88f77e8d572 1 #include <mbed.h>
tass 0:b88f77e8d572 2 #include <stdarg.h>
tass 0:b88f77e8d572 3 #include "EthernetInterface.h"
tass 0:b88f77e8d572 4
tass 0:b88f77e8d572 5 #define ECHO_SERVER_PORT 7
tass 0:b88f77e8d572 6 #define BUFFER_QUANTITY (1024*1024)
tass 0:b88f77e8d572 7 #define MAX_NUMBER_OF_RETRIES (10u)
tass 0:b88f77e8d572 8
tass 0:b88f77e8d572 9
tass 0:b88f77e8d572 10 int main() {
tass 0:b88f77e8d572 11
tass 0:b88f77e8d572 12 printf("Socket exchange data forever...\n");
tass 0:b88f77e8d572 13
tass 0:b88f77e8d572 14 EthernetInterface eth;
tass 0:b88f77e8d572 15 int connections = 0;
tass 0:b88f77e8d572 16 eth.init(); //Use DHCP
tass 0:b88f77e8d572 17 eth.connect();
tass 0:b88f77e8d572 18 //printf("IP Address %s\n", eth.getIPAddress());
tass 0:b88f77e8d572 19
tass 0:b88f77e8d572 20 TCPSocketServer server;
tass 0:b88f77e8d572 21 server.bind(ECHO_SERVER_PORT);
tass 0:b88f77e8d572 22 server.listen();
tass 0:b88f77e8d572 23
tass 0:b88f77e8d572 24 while (true) {
tass 0:b88f77e8d572 25 printf("\nWait for new connection...\n");
tass 0:b88f77e8d572 26 printf("Client number %d\n",++connections);
tass 0:b88f77e8d572 27 TCPSocketConnection client;
tass 0:b88f77e8d572 28 server.accept(client);
tass 0:b88f77e8d572 29 client.set_blocking(false, 2500); // Timeout after (1.5)s
tass 0:b88f77e8d572 30 printf("Connection from: %s\n", client.get_address());
tass 0:b88f77e8d572 31 char buffer[1024];
tass 0:b88f77e8d572 32 while (true) {
tass 0:b88f77e8d572 33 int retries = 0;
tass 0:b88f77e8d572 34 int dataReceived = 0;
tass 0:b88f77e8d572 35 int dataSent = 0;
tass 0:b88f77e8d572 36
tass 0:b88f77e8d572 37 printf("\n\n\nStarting the receiving part...\n");
tass 0:b88f77e8d572 38 while(dataReceived < BUFFER_QUANTITY)
tass 0:b88f77e8d572 39 {
tass 0:b88f77e8d572 40 int n = client.receive(buffer, sizeof(buffer));
tass 0:b88f77e8d572 41 if (n <= 0) {
tass 0:b88f77e8d572 42 printf("Receive error\n");
tass 0:b88f77e8d572 43 retries++;
tass 0:b88f77e8d572 44 if(retries >= MAX_NUMBER_OF_RETRIES)
tass 0:b88f77e8d572 45 break;
tass 0:b88f77e8d572 46 }
tass 0:b88f77e8d572 47 dataReceived += n;
tass 0:b88f77e8d572 48 }
tass 0:b88f77e8d572 49
tass 0:b88f77e8d572 50
tass 0:b88f77e8d572 51 printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY);
tass 0:b88f77e8d572 52 if(dataReceived < BUFFER_QUANTITY)
tass 0:b88f77e8d572 53 {
tass 0:b88f77e8d572 54 printf("Receiving part of the test has failed. Exiting connection.\n");
tass 0:b88f77e8d572 55 break;
tass 0:b88f77e8d572 56 }
tass 0:b88f77e8d572 57 else{
tass 0:b88f77e8d572 58 printf("Receiving has passed...\n");
tass 0:b88f77e8d572 59 }
tass 0:b88f77e8d572 60
tass 0:b88f77e8d572 61 printf("\n\n\nStarting the sending part...\n");
tass 0:b88f77e8d572 62 retries = 0;
tass 0:b88f77e8d572 63 while(dataSent < BUFFER_QUANTITY)
tass 0:b88f77e8d572 64 {
tass 0:b88f77e8d572 65 int n = client.send_all(buffer, sizeof(buffer));
tass 0:b88f77e8d572 66 if (n <= 0) {
tass 0:b88f77e8d572 67 printf("Send error\n");
tass 0:b88f77e8d572 68 retries++;
tass 0:b88f77e8d572 69 if(retries >= MAX_NUMBER_OF_RETRIES)
tass 0:b88f77e8d572 70 break;
tass 0:b88f77e8d572 71 }
tass 0:b88f77e8d572 72 dataSent += n;
tass 0:b88f77e8d572 73 }
tass 0:b88f77e8d572 74
tass 0:b88f77e8d572 75 printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY);
tass 0:b88f77e8d572 76 if(dataSent < BUFFER_QUANTITY)
tass 0:b88f77e8d572 77 {
tass 0:b88f77e8d572 78 printf("Sending part of the test has failed. Exiting connection.\n");
tass 0:b88f77e8d572 79 break;
tass 0:b88f77e8d572 80 }
tass 0:b88f77e8d572 81 else
tass 0:b88f77e8d572 82 {
tass 0:b88f77e8d572 83 printf("Sending test has passed...\n");
tass 0:b88f77e8d572 84 }
tass 0:b88f77e8d572 85
tass 0:b88f77e8d572 86
tass 0:b88f77e8d572 87 printf("\n\n\nStarting echo part...\n");
tass 0:b88f77e8d572 88 dataReceived = dataSent = 0;
tass 0:b88f77e8d572 89
tass 0:b88f77e8d572 90 while((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:b88f77e8d572 91 {
tass 0:b88f77e8d572 92 int n = client.receive(buffer, sizeof(buffer));
tass 0:b88f77e8d572 93 if (n <= 0) {
tass 0:b88f77e8d572 94 printf("Receive error\n");
tass 0:b88f77e8d572 95 }
tass 0:b88f77e8d572 96 dataReceived += n;
tass 0:b88f77e8d572 97
tass 0:b88f77e8d572 98 n = client.send_all(buffer, n);
tass 0:b88f77e8d572 99 if (n <= 0) {
tass 0:b88f77e8d572 100 printf("Send error, returned 0\n");
tass 0:b88f77e8d572 101 break;
tass 0:b88f77e8d572 102 }
tass 0:b88f77e8d572 103 dataSent += n;
tass 0:b88f77e8d572 104 }
tass 0:b88f77e8d572 105
tass 0:b88f77e8d572 106 printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY);
tass 0:b88f77e8d572 107 if((dataReceived+dataSent) < 2*BUFFER_QUANTITY)
tass 0:b88f77e8d572 108 {
tass 0:b88f77e8d572 109 printf("Echo test has failed.Exiting connection...\n");
tass 0:b88f77e8d572 110 break;
tass 0:b88f77e8d572 111 }
tass 0:b88f77e8d572 112 }
tass 0:b88f77e8d572 113
tass 0:b88f77e8d572 114 client.close();
tass 0:b88f77e8d572 115 printf("Test was finished...\n");
tass 0:b88f77e8d572 116
tass 0:b88f77e8d572 117 }
tass 0:b88f77e8d572 118
tass 0:b88f77e8d572 119 return 0;
tass 0:b88f77e8d572 120 }