/* Small echo service. It listen on a port and copy back what is sent */ #include #include #include #include #include #include #include #include #include #include struct CLIENT_INFO: public ARRAY_OBJ{ long received; time_t start; CLIENT_INFO(){ start = time(NULL); received = 0; } }; int main (int argc, char *argv[]) { glocal bool server = false; glocal int count = 100000; glocal const char *echoserver = NULL; glocal bool slowserver = false; glocal const char *manyconnect = NULL; glocal int nbprocess = 1; glocal const char *client = NULL; glocal const char *bind = NULL; glocal const char *port = NULL; glocal const char *message = "ECHO"; glocal bool pingmode = false; glocal bool discard = false; // Do not analyse the output of the echo server // for manyconnect int ret = (argc, argv); setarg ('b',"bind","bind on this IP",glocal.bind,false); setarg ('p',"port","Setup the service on this port",glocal.port,true); setarg ('s',"server","this is the server/listener",glocal.server,false); setarg ('c',"client","Client mode, specify the server",glocal.client,false); setarg (' ',"echoserver","Client mode, specify the echo server",glocal.echoserver,false); setarg (' ',"slowserver","Slow server, receive and usleep",glocal.slowserver,false); setarg ('C',"count","Number of packets to send",glocal.count,false); setarg ('P',"pingmode","exchange packets between client and server",glocal.pingmode,false); setgrouparg ("Manyconnect"); setarg (' ',"manyconnect","Establish many short connection to the echo server",glocal.manyconnect,false); setarg (' ',"processes","Number of processes (for manyconnect)",glocal.nbprocess,false); setarg (' ',"discard","Do not parse the response from the server",glocal.discard,false); int ret = -1; if (glocal.server){ (glocal.bind,glocal.port,1); settcpnodelay (true); info.data = new CLIENT_INFO(); CLIENT_INFO *n = (CLIENT_INFO*)info.data; int duration = time(NULL) - n->start; printf ("Received %ld in %d seconds\n",n->received,duration); if (duration > 1){ printf ("rate=%lf bytes/s\n",(double)n->received/duration); } CLIENT_INFO *n = (CLIENT_INFO*)info.data; n->received += info.linelen; if (glocal.pingmode) send (line,info.linelen); s.setrawmode (true); s.loop(); ret = 0; }else if (glocal.slowserver){ (glocal.bind,glocal.port,1); settcpnodelay (true); info.data = new CLIENT_INFO(); CLIENT_INFO *n = (CLIENT_INFO*)info.data; printf ("Received %ld\n",n->received); CLIENT_INFO *n = (CLIENT_INFO*)info.data; n->received += info.linelen; usleep (100000); s.setrawmode (true); s.loop(); ret = 0; }else if (glocal.client != NULL){ glocal long received = 0; (glocal.bind,glocal.client,glocal.port,1); settcpnodelay(true); setrawmode (true); if (glocal.pingmode){ char buf[100]; send (buf,sizeof(buf)); }else{ char buf[10000]; for (int i=0; i glocal.received += info.linelen; glocal.count--; if (glocal.count == 0){ end = true; }else{ char buf[100]; send (buf,sizeof(buf)); } printf ("received %ld\n",glocal.received); ret = 0; }else if (glocal.echoserver != NULL){ glocal const char *longstr = "1222222222222222222222222222222222222222222222222" "22222222222222222222222222222222222222222222222222222222222222222"; glocal long received = 0; glocal bool first = true; long expected = glocal.count*(long)(strlen(glocal.longstr)+6); (glocal.bind,glocal.echoserver,glocal.port,1); settcpnodelay(true); if (fork()==0){ for (int i=0; i if (glocal.first){ glocal.first = false; // Skip the welcome }else{ glocal.received += info.linelen; glocal.count--; if (glocal.count == 0){ end = true; } } printf ("received %ld expected %ld\n",glocal.received,expected); if (glocal.received != expected) fprintf (stderr,"****** mismatch ******\n"); ret = 0; }else if (glocal.manyconnect != NULL){ for (int p=0; p(glocal.bind,glocal.manyconnect,glocal.port,1); settcpnodelay(true); send ("hello\n"); if (!glocal.discard){ //printf ("Receive %d %d: %s\n",glocal.welcome_seen,glocal.hello_seen,line); if (strncmp(line,"Welcome",7)==0){ glocal.welcome_seen = true; }else if (strncmp(line,"echo hello",10)==0){ if (!glocal.welcome_seen) tlmp_error ("Received hello without the welcome\n"); glocal.hello_seen = true; end = true; }else{ tlmp_error ("Received unexpected line: %s\n",line); end = true; } } if (!glocal.discard) printf ("%d-%d\n",p,i); } _exit (0); }else if (pid == (pid_t)-1){ tlmp_error ("Can't fork (%s)\n",strerror(errno)); exit (-1); } } int status; while (wait(&status)!=-1); ret = 0; }else{ tlmp_error ("You must select either --server or --client or --echoserver servername mode\n"); } return ret; return ret; }