ttcp.c - test ttcp speed
Terry Slattery <tcs@usna>
tcs at usna.UUCP
Fri Feb 8 11:52:33 AEST 1985
Several folks requested the source to this. I got the 4.1 source from
George Powers at Excelan and converted it to 4.2. They have a newer
version that checks packets on receipt. I prefer to use this version
for testing boards due to the lower cpu overhead.
-tcs
Terry Slattery U.S. Naval Academy 301-267-4413
ARPA: tcs at brl-bmd UUCP: decvax!brl-bmd!usna!tcs
========================= ttcp.c ==================================
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
struct sockaddr_in sinme;
struct sockaddr_in sinhim;
struct sockaddr_in sindum;
struct sockaddr_in frominet;
int s, domain, fromlen;
int fd, fdr, fdw;
extern int errno;
char buf[1024];
int cnt;
int options = 0;
short port = 2000, portoff = 0;
char *cp, *host, trans, *argv1 = "";
int zsw; /* send zeros */
long rhost();
struct hostent *addr;
/*
* ttcp - test tcp
*
* usage: ttcp [t|r|td|rd|tznnnn|rz] [host] [portoffset] <in >out
* t = transmit
* r = receive
* td = transmit with debugging enabled
* rd = receive with debugging enabled
* tznnnn = transmit nnnn 1K byte zero filled blocks
* rz = receive data and drop it
* ...nnnn is number of zero filled 1024 byte buffers to xmit...
*
* host = name of host to connect to as described in /etc/hosts
* portoffset = offset of the port number (default to zero
* which selects port 2000)
*
* modified for operation under 4.2BSD, 18 Dec 84
* T.C. Slattery, USNA
*/
main(argc,argv)
char **argv;
{
if (argc < 2) goto usage;
if (argc >= 4) portoff = atoi(argv[3]);
switch (argv[1][0]) {
case 't':
trans++;
if (argc < 3) goto usage;
bzero((char *)&sinhim, sizeof(sinhim));
if (atoi(argv[2]) > 0 ) {
/* Numeric */
sinhim.sin_family = AF_INET;
sinhim.sin_addr.s_addr = inet_addr(argv[2]);
} else {
if ((addr=gethostbyname(argv[2])) == NULL)
err("bad hostname");
sinhim.sin_family = addr->h_addrtype;
bcopy(addr->h_addr,(char*)&sinhim.sin_addr, addr->h_length);
}
sinhim.sin_port = htons(port+portoff);
sinme.sin_port = htons((port<<1)+portoff);
break;
case 'r':
sinme.sin_port = htons(port+portoff);
break;
default:
goto usage;
}
argv1 = argv[1];
if (argv[1][1] == 'd') options |= SO_DEBUG;
if (argv[1][1] == 'z') zsw = 1000;
if (trans)
if (argv[1][1] == 'z') zsw = atoi(argv[1]+2);
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err("socket");
fprintf (stderr, "socket fd= %d\n", fd);
printf("ttcp:buf size=%d\n",sizeof(buf));
mes("socket");
bind(fd, &sinme, sizeof(sinme));
if (trans) {
/* We are the client if transmitting */
if(options)
if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
err("setsockopt");
if(connect(fd, &sinhim, sizeof(sinhim) ) < 0)
err("connect");
fdr = 0; fdw = fd; /* fdr = stdin, fdw = socket */
mes("connect");
} else {
/* otherwise, we are the server and
* should listen for the connections
*/
listen(fd,2); /* allow a queue of 2 */
if(options)
if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
err("setsockopt");
fromlen = sizeof(frominet);
domain = AF_INET;
if((fdr = accept(fd, &frominet, &fromlen) ) < 0)
err("accept");
fdw = 1; /* fdr = socket, fdw = stdout */
mes("accept");
}
errno = 0;
if (zsw) {
if (trans)
while (zsw-- && write(fdw,buf,sizeof buf) == sizeof buf);
else
while (read(fdr,buf,sizeof buf) > 0);
}
else
while((cnt=read(fdr,buf,sizeof buf)) > 0 &&
write(fdw,buf,cnt) == cnt);
if(errno) err("IO");
exit(0);
usage:
err("usage: ttcp [t|r|td|tr|rz|tznnnn] [host] [portoffset] <in >out");
}
err(s)
char *s;
{
fprintf(stderr,"ttcp%s: error %s\n", argv1, s);
perror("");
fprintf(stderr,"errno=%d\n",errno);
exit(1);
}
mes(s)
char *s;
{
fprintf(stderr,"ttcp%s: %s\n", argv1, s);
}
More information about the Comp.sources.unix
mailing list