/* pingclient.c - a client program that uses the socket interface to tcp */ #include #include #include #include #include #include #include #include #include #include "client.h" #include "tcpblockio.h" #include "timer.h" #define ITERATIONS 100 #define AMOUNT 1 #define MAXAMOUNT 10000000 char data[MAXAMOUNT]; void client( int server_port, char *server_node, int argc, char **argv ) { int count, iterations=0, amount=0; int fd, nettemp; struct sockaddr_in server_addr, client_addr; char local_node[MAXHOSTNAMELEN]; struct tms tmsstart, tmsstop; clock_t start, stop; if( argc > 0 ) iterations = atoi(argv[0]); if( iterations <= 0 ) iterations = ITERATIONS; if( argc > 1 ) amount = atoi(argv[1]); if( amount <= 0 ) amount = AMOUNT; if( amount > MAXAMOUNT ) amount = MAXAMOUNT; fprintf(stderr, "pingclient: %d iterations, %d bytes each\n", iterations, amount); /* 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, "pingclient: on host %s\n", local_node); fd = openclient( server_port, server_node, &server_addr, &client_addr); /* we are now successfully connected to a remote server */ fprintf(stderr, "pingclient: 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), server_port); /* first tell server the size of each data message, in bytes */ nettemp = htonl(amount); if( writeblock(fd, (char *)&nettemp, sizeof(nettemp)) != sizeof(nettemp) ) exit(EXIT_FAILURE); if( (start = times(&tmsstart)) < 0 ) exit(EXIT_FAILURE); /* now transmit data messages to server */ for( count = 0; count < iterations; count++ ) { if( writeblock(fd, data, amount) < 0 ) exit(EXIT_FAILURE); if( readblock(fd, data, amount) <= 0 ) break; } if( (stop = times(&tmsstop)) < 0 ) exit(EXIT_FAILURE); fprintf(stderr, "pingclient: finished %d iterations of %d bytes each\n", count, amount); pr_times(stderr, stop-start, &tmsstart, &tmsstop); /* close the connection to the server */ if( close(fd) < 0 ) { perror("pingclient close"); exit(EXIT_FAILURE); } }