/* pingclient.c - a client program that uses the socket interface to tcp */ #include #include #include #include #include #include #include #include #include #include #include #include "client.h" #include "tcpblockio.h" #include "timer.h" #define TIMES 10 #define STEPS 40 #define STARTPOINT 0 #define ENDPOINT 0.16 #define SET_RATE 1 #define RECORD_LOSS 2 char data[1024]; int main( int argc, char **argv ) { int amount=0; int fd, nettemp; struct sockaddr_in server_addr, client_addr; char local_node[MAXHOSTNAMELEN]; float startPoint; float endPoint, dropRate, rateDiff; int status, i, steps=40; pid_t pid; if ( argc != 6 ) { fprintf(stderr, "usage: %s port# IPaddress startRate endRate Steps\n", argv[ 0 ] ); exit( 1 ); } startPoint = atof( argv[3] ); if (startPoint <= 0) startPoint = STARTPOINT; endPoint = atof( argv[4] ); if (endPoint <=0 ) endPoint = ENDPOINT; if ( endPoint < startPoint ) { fprintf(stderr, "endpoint should not be less than startpoint\n"); exit( 1 ); } steps = atoi( argv[5] ); if ( steps <= 0 ) { steps = STEPS; } /* get the internet name of the local host node on which we are running */ if( gethostname(local_node, MAXHOSTNAMELEN) < 0 ) { perror("pingclient gethostname"); exit(EXIT_FAILURE); } fprintf(stderr, "myclient: on host %s\n", local_node); fd = openclient( 5678, "dublin.cs.unh.edu", &server_addr, &client_addr); /* we are now successfully connected to a remote server */ fprintf(stderr, "myclient: at internet address %s, port %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); fprintf(stderr, "pingclient: sending to server at internet address %s, port %d\n", inet_ntoa(server_addr.sin_addr), 5678); rateDiff = ( endPoint - startPoint ) / steps; /* outer for loop set dropping rate */ for ( dropRate=startPoint; dropRate <= endPoint; dropRate += rateDiff ) { /* printf rate to a buffer */ fprintf(stderr, "\nDublin packet drop rate is set to: %f\n\n", dropRate ); sprintf( data, "%d %.8f", SET_RATE, dropRate ); /* Tell server the size of data */ amount = strlen( data ); nettemp = htonl( amount ); if( writeblock(fd, (char *)&nettemp, sizeof(nettemp)) != sizeof(nettemp) ) exit( EXIT_FAILURE ); /* write amount bytes data */ if ( writeblock( fd, data, amount ) < 0 ) exit(EXIT_FAILURE); /* server will echo the message received after do what it should do */ if( readblock(fd, data, amount) <= 0 ) break; /* inner loop run blasttest for TIMES times */ for( i = 0; i < TIMES; i++ ) { /* fork a child to run blastclient */ if( (pid = vfork()) < 0 ) exit( EXIT_FAILURE ); else if ( pid > 0 ) { /* parent process just waits for child to stop and restart */ wait(&status); /* parent process tell server to record the packet loss */ /* printf rate to a buffer */ sprintf( data, "%d", RECORD_LOSS); /* Tell server the size of data */ amount = strlen( data ); nettemp = htonl( amount ); if( writeblock(fd, (char *)&nettemp, sizeof(nettemp)) != sizeof(nettemp) ) exit( EXIT_FAILURE ); /* write amount bytes data */ if ( writeblock( fd, data, amount ) < 0 ) exit(EXIT_FAILURE); /* server will echo the message received after do * what it should do */ if( readblock(fd, data, amount) <= 0 ) exit(EXIT_FAILURE); continue; }else { /* child process execute blasttest */ fprintf(stderr, "\n\nblastclient 1026 192.168.2.2 100000 1448\n" ); execlp("blastclient","blastclient", "1026", "192.168.2.2", "100000", "1448", NULL ); exit( 0 ); } }/* end of inner for loop */ }/* end of outer for loop */ return 0; }/* end of main */