📄 ttcp.c
字号:
/*
* T T C P . C
*
* Test TCP connection. Makes a connection on port 5001
* and transfers fabricated buffers or data copied from stdin.
*
* Usable on 4.2, 4.3, and 4.1a systems by defining one of
* BSD42 BSD43 (BSD41a)
* Machines using System V with BSD sockets should define SYSV.
*
* Modified for operation under 4.2BSD, 18 Dec 84
* T.C. Slattery, USNA
* Minor improvements, Mike Muuss and Terry Slattery, 16-Oct-85.
* Modified in 1989 at Silicon Graphics, Inc.
* catch SIGPIPE to be able to print stats when receiver has died
* for tcp, don't look for sentinel during reads to allow small transfers
* increased default buffer size to 8K, nbuf to 2K to transfer 16MB
* moved default port to 5001, beyond IPPORT_USERRESERVED
* make sinkmode default because it is more popular,
* -s now means don't sink/source
* count number of read/write system calls to see effects of
* blocking from full socket buffers
* for tcp, -D option turns off buffered writes (sets TCP_NODELAY sockopt)
* buffer alignment options, -A and -O
* print stats in a format that's a bit easier to use with grep & awk
* for SYSV, mimic BSD routines to use most of the existing timing code
* Modified by Steve Miller of the University of Maryland, College Park
* -b sets the socket buffer size (SO_SNDBUF/SO_RCVBUF)
* Modified Sept. 1989 at Silicon Graphics, Inc.
* restored -s sense at request of tcs@brl
* Modified Oct. 1991 at Silicon Graphics, Inc.
* use getopt(3) for option processing, add -f and -T options.
* SGI IRIX 3.3 and 4.0 releases don't need #define SYSV.
*
* Distribution Status -
* Public Domain. Distribution Unlimited.
*/
#ifndef lint
static char RCSid[] = "ttcp.c $Revision: 1.12 $";
#endif
#define BSD43
/* #define BSD42 */
/* #define BSD41a */
/* #define SYSV */ /* required on SGI IRIX releases before 3.3 */
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h> /* struct timeval */
#if defined(SYSV)
#include <sys/times.h>
#include <sys/param.h>
struct rusage {
struct timeval ru_utime, ru_stime;
};
#define RUSAGE_SELF 0
#else
#include <sys/resource.h>
#endif
struct sockaddr_in sinme;
struct sockaddr_in sinhim;
struct sockaddr_in frominet;
int domain, fromlen;
int fd; /* fd of network socket */
int buflen = 8 * 1024; /* length of buffer */
char *buf; /* ptr to dynamic buffer */
int nbuf = 2 * 1024; /* number of buffers to send in sinkmode */
int bufoffset = 0; /* align buffer to this */
int bufalign = 16*1024; /* modulo this */
int udp = 0; /* 0 = tcp, !0 = udp */
int options = 0; /* socket options */
int one = 1; /* for 4.3 BSD style setsockopt() */
short port = 5001; /* TCP port number */
char *host; /* ptr to name of host */
int trans; /* 0=receive, !0=transmit mode */
int sinkmode = 1; /* 0=normal I/O, !0=sink/source mode */
int verbose = 0; /* 0=print basic info, 1=print cpu rate, proc
* resource usage. */
int nodelay = 1; /* set TCP_NODELAY socket option */
int b_flag = 0; /* use mread() */
int sockbufsize = 0; /* socket buffer size to use */
char fmt = 'K'; /* output format: k = kilobits, K = kilobytes,
* m = megabits, M = megabytes,
* g = gigabits, G = gigabytes */
int touchdata = 0; /* access data after reading */
struct hostent *addr;
extern int errno;
extern int optind;
extern char *optarg;
char Usage[] = "\
Usage: ttcp -t [-options] host [ < in ]\n\
ttcp -r [-options > out]\n\
Common options:\n\
-l ## length of bufs read from or written to network (default 8192)\n\
-u use UDP instead of TCP\n\
-p ## port number to send to or listen at (default 5001)\n\
-s -t: source a pattern to network\n\
-r: sink (discard) all data from network\n\
-A align the start of buffers to this modulus (default 16384)\n\
-O start buffers at this offset from the modulus (default 0)\n\
-v verbose: print more statistics\n\
-d set SO_DEBUG socket option\n\
-b ## set socket buffer size (if supported)\n\
-f X format for rate: k,K = kilo{bit,byte}; m,M = mega; g,G = giga\n\
Options specific to -t:\n\
-n## number of source bufs written to network (default 2048)\n\
-D don't buffer TCP writes (sets TCP_NODELAY socket option)\n\
Options specific to -r:\n\
-B for -s, only output full blocks as specified by -l (for TAR)\n\
-T \"touch\": access each byte as it's read\n\
";
char stats[128];
double nbytes; /* bytes on net */
unsigned long numCalls; /* # of I/O system calls */
double cput, realt; /* user, real time (seconds) */
void err();
void mes();
int pattern();
void prep_timer();
double read_timer();
int Nread();
int Nwrite();
void delay();
int mread();
char *outfmt();
void
sigpipe()
{
}
main(argc,argv)
int argc;
char **argv;
{
unsigned long addr_tmp;
int c;
if (argc < 2) goto usage;
while ((c = getopt(argc, argv, "drstuvBDTb:f:l:n:p:A:O:")) != -1) {
switch (c) {
case 'B':
b_flag = 1;
break;
case 't':
trans = 1;
break;
case 'r':
trans = 0;
break;
case 'd':
options |= SO_DEBUG;
break;
case 'D':
#ifdef TCP_NODELAY
nodelay = 1;
#else
fprintf(stderr,
"ttcp: -D option ignored: TCP_NODELAY socket option not supported\n");
#endif
break;
case 'n':
nbuf = atoi(optarg);
break;
case 'l':
buflen = atoi(optarg);
break;
case 's':
sinkmode = !sinkmode;
break;
case 'p':
port = atoi(optarg);
break;
case 'u':
udp = 1;
break;
case 'v':
verbose = 1;
break;
case 'A':
bufalign = atoi(optarg);
break;
case 'O':
bufoffset = atoi(optarg);
break;
case 'b':
#if defined(SO_SNDBUF) || defined(SO_RCVBUF)
sockbufsize = atoi(optarg);
#else
fprintf(stderr,
"ttcp: -b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported\n");
#endif
break;
case 'f':
fmt = *optarg;
break;
case 'T':
touchdata = 1;
break;
default:
goto usage;
}
}
if(trans) {
/* xmitr */
if (optind == argc)
goto usage;
bzero((char *)&sinhim, sizeof(sinhim));
host = argv[optind];
if (atoi(host) > 0 ) {
/* Numeric */
sinhim.sin_family = AF_INET;
#if defined(cray)
addr_tmp = inet_addr(host);
sinhim.sin_addr = addr_tmp;
#else
sinhim.sin_addr.s_addr = inet_addr(host);
#endif
} else {
if ((addr=gethostbyname(host)) == NULL)
err("bad hostname");
sinhim.sin_family = addr->h_addrtype;
bcopy(addr->h_addr,(char*)&addr_tmp, addr->h_length);
#if defined(cray)
sinhim.sin_addr = addr_tmp;
#else
sinhim.sin_addr.s_addr = addr_tmp;
#endif /* cray */
}
sinhim.sin_port = htons(port);
sinme.sin_family = AF_INET;
sinme.sin_port = 0; /* free choice */
} else {
/* rcvr */
sinme.sin_family = AF_INET;
sinme.sin_port = htons(port);
}
if (udp && buflen < 5) {
buflen = 5; /* send more than the sentinel size */
}
if ( (buf = (char *)malloc(buflen+bufalign)) == (char *)NULL)
err("malloc");
if (bufalign != 0)
buf +=(bufalign - ((int)buf % bufalign) + bufoffset) % bufalign;
if (trans) {
fprintf(stdout,
"ttcp-t: buflen=%d, nbuf=%d, align=%d/%d, port=%d",
buflen, nbuf, bufalign, bufoffset, port);
if (sockbufsize)
fprintf(stdout, ", sockbufsize=%d", sockbufsize);
fprintf(stdout, " %s -> %s\n", udp?"udp":"tcp", host);
} else {
fprintf(stdout,
"ttcp-r: buflen=%d, nbuf=%d, align=%d/%d, port=%d",
buflen, nbuf, bufalign, bufoffset, port);
if (sockbufsize)
fprintf(stdout, ", sockbufsize=%d", sockbufsize);
fprintf(stdout, " %s\n", udp?"udp":"tcp");
}
if ((fd = socket(AF_INET, udp?SOCK_DGRAM:SOCK_STREAM, 0)) < 0)
err("socket");
mes("socket");
if (bind(fd, &sinme, sizeof(sinme)) < 0)
err("bind");
#if defined(SO_SNDBUF) || defined(SO_RCVBUF)
if (sockbufsize) {
if (trans) {
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
sizeof sockbufsize) < 0)
err("setsockopt: sndbuf");
mes("sndbuf");
} else {
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
sizeof sockbufsize) < 0)
err("setsockopt: rcvbuf");
mes("rcvbuf");
}
}
#endif
if (!udp)
{
signal(SIGPIPE, sigpipe);
if (trans)
{
/* We are the client if transmitting */
if (options)
{
#if defined(BSD42)
if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
#else /* BSD43 */
if( setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one)) < 0)
#endif
err("setsockopt");
}
#ifdef TCP_NODELAY
if (nodelay)
{
struct protoent *p;
p = getprotobyname("tcp");
if( p && setsockopt(fd, p->p_proto, TCP_NODELAY,
&one, sizeof(one)) < 0)
{
err("setsockopt: nodelay");
}
mes("nodelay");
}
#endif
if(connect(fd, &sinhim, sizeof(sinhim) ) < 0)
err("connect");
mes("connect");
}
else
{
/* otherwise, we are the server and
* should listen for the connections
*/
#if defined(ultrix) || defined(sgi)
listen(fd,1); /* workaround for alleged u4.2 bug */
#else
listen(fd,0); /* allow a queue of 0 */
#endif
if(options)
{
#if defined(BSD42)
if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
#else /* BSD43 */
if( setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one)) < 0)
#endif
err("setsockopt");
}
fromlen = sizeof(frominet);
domain = AF_INET;
if((fd=accept(fd, &frominet, &fromlen) ) < 0)
err("accept");
{
struct sockaddr_in peer;
int peerlen = sizeof(peer);
if (getpeername(fd, (struct sockaddr_in *) &peer,
&peerlen) < 0)
{
err("getpeername");
}
fprintf(stderr,"ttcp-r: accept from %s\n",
inet_ntoa(peer.sin_addr));
}
}
}
prep_timer();
errno = 0;
if (sinkmode)
{
register int cnt;
if (trans)
{
pattern( buf, buflen );
if(udp) (void)Nwrite( fd, buf, 4 ); /* rcvr start */
while (nbuf-- && Nwrite(fd,buf,buflen) == buflen)
nbytes += buflen;
if(udp) (void)Nwrite( fd, buf, 4 ); /* rcvr end */
}
else
{
if (udp)
{
while ((cnt=Nread(fd,buf,buflen)) > 0)
{
static int going = 0;
if( cnt <= 4 )
{
if( going )
break; /* "EOF" */
going = 1;
prep_timer();
}
else
{
nbytes += cnt;
}
}
}
else
{
while ((cnt=Nread(fd,buf,buflen)) > 0)
{
nbytes += cnt;
}
}
}
}
else
{
register int cnt;
if (trans)
{
while((cnt=read(0,buf,buflen)) > 0 &&
Nwrite(fd,buf,cnt) == cnt)
nbytes += cnt;
}
else
{
while((cnt=Nread(fd,buf,buflen)) > 0 &&
write(1,buf,cnt) == cnt)
nbytes += cnt;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -