/* * bytes_tcp.c - simple TCP bandwidth source/sink * * server usage: bytes_tcp -s * client usage: bytes_tcp hostname [msgsize] * * Copyright (c) 1994 Larry McVoy. * Copyright (c) 2002 Carl Staelin. Distributed under the FSF GPL with * additional restriction that results may published only if * (1) the benchmark is unmodified, and * (2) the version in the sccsid below is included in the report. * Support for this development by Sun Microsystems is gratefully acknowledged. */ char *id = "$Id$\n"; #include "bench.h" #define XFER (1024*1024) int server_main(int ac, char **av); int client_main(int ac, char **av); void source(int data); void transfer(int get, int server, char *buf) { int c; while ((get > 0) && (c = read(server, buf, XFER)) > 0) { get -= c; } if (c < 0) { perror("bytes_tcp: transfer: read failed"); exit(4); } } /* ARGSUSED */ int client_main(int ac, char **av) { int server; int get = 256 << 20; char buf[XFER]; char* usage = "usage: %s -remotehost OR %s remotehost [msgsize]\n"; if (ac != 2 && ac != 3) { (void)fprintf(stderr, usage, av[0], av[0]); exit(0); } if (ac == 3) get = bytes(av[2]); server = tcp_connect(av[1], TCP_DATA+1, SOCKOPT_READ|SOCKOPT_REUSE); if (server < 0) { perror("bytes_tcp: could not open socket to server"); exit(2); } transfer(get, server, buf); close(server); exit(0); /*NOTREACHED*/ } void child() { wait(0); signal(SIGCHLD, child); } /* ARGSUSED */ int server_main(int ac, char **av) { int data, newdata; signal(SIGCHLD, child); data = tcp_server(TCP_DATA+1, SOCKOPT_READ|SOCKOPT_WRITE|SOCKOPT_REUSE); for ( ;; ) { newdata = tcp_accept(data, SOCKOPT_WRITE|SOCKOPT_READ); switch (fork()) { case -1: perror("fork"); break; case 0: source(newdata); exit(0); default: close(newdata); break; } } } void source(int data) { char buf[XFER]; while (write(data, buf, sizeof(buf)) > 0); } int main(int ac, char **av) { char* usage = "Usage: %s -s OR %s -serverhost OR %s serverhost [msgsize]\n"; if (ac < 2 || 3 < ac) { fprintf(stderr, usage, av[0], av[0], av[0]); exit(1); } if (ac == 2 && !strcmp(av[1], "-s")) { if (fork() == 0) server_main(ac, av); exit(0); } else { client_main(ac, av); } return(0); }