mirror of https://framagit.org/bortzmeyer/echoping
Removed bogus directory
parent
936a7bf78f
commit
aab38992d2
@ -1,71 +0,0 @@
|
|||||||
# Change the following as required:
|
|
||||||
CC = gcc
|
|
||||||
|
|
||||||
CFLAGS = -Wall
|
|
||||||
#CFLAGS = -ansi -Wall -Dsun -D__STDC__=0 -DMSG_EOF=0
|
|
||||||
# My flags for gcc/solaris 2.3: -ansi -Wall -Dsun -D__STDC__=0
|
|
||||||
# (Solaris -DMSG_EOF just to let me compile it under Solaris; it won't run)
|
|
||||||
# Add in -DGCC_STRUCT_PROBLEM for gcc versions 1.x under SunOS 4.x
|
|
||||||
# Add in -D__STDC__=0 for gcc under Solaris 2 (for Sun's screwy headers)
|
|
||||||
# Add in -D_BSD=44 for AIX 3.2.2 (see <sys/socket.h>)
|
|
||||||
# Add in -D_SOCKADDR_LEN for DEC OSF/1 (see <sys/socket.h>)
|
|
||||||
|
|
||||||
# Following line for SVR4, Solaris 2.x; 2.5 no longer requires libucb.a.
|
|
||||||
#LIBS = ./libmisc.a /usr/ucblib/libucb.a -lsocket -lnsl
|
|
||||||
|
|
||||||
# Following line for 4.4BSD, BSD/386, SunOS 4.x, AIX 3.2.2, OSF/1
|
|
||||||
LIBS = ./libmisc.a
|
|
||||||
|
|
||||||
PROGS = udpcli udpserv tcpcli tcpserv ttcpcli ttcpserv \
|
|
||||||
ttcpclibig ttcpservbig ttcphttpcli \
|
|
||||||
udpclitime udpservtime tcpclitime tcpservtime ttcpclitime ttcpservtime
|
|
||||||
|
|
||||||
all: ${PROGS}
|
|
||||||
|
|
||||||
udpcli: udpcli.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ udpcli.o ${LIBS}
|
|
||||||
|
|
||||||
udpserv: udpserv.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ udpserv.o ${LIBS}
|
|
||||||
|
|
||||||
tcpcli: tcpcli.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ tcpcli.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
tcpserv: tcpserv.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ tcpserv.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
ttcpcli: ttcpcli.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcpcli.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
ttcpserv: ttcpserv.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcpserv.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
ttcpclibig: ttcpclibig.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcpclibig.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
ttcpservbig: ttcpservbig.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcpservbig.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
ttcphttpcli: ttcphttpcli.o readstream.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcphttpcli.o readstream.o ${LIBS}
|
|
||||||
|
|
||||||
udpclitime: udpclitime.o timer.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ udpclitime.o timer.o ${LIBS}
|
|
||||||
|
|
||||||
udpservtime: udpservtime.o timer.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ udpservtime.o timer.o ${LIBS}
|
|
||||||
|
|
||||||
tcpclitime: tcpclitime.o readstream.o timer.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ tcpclitime.o readstream.o timer.o ${LIBS}
|
|
||||||
|
|
||||||
tcpservtime: tcpservtime.o readstream.o timer.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ tcpservtime.o readstream.o timer.o ${LIBS}
|
|
||||||
|
|
||||||
ttcpclitime: ttcpclitime.o readstream.o timer.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcpclitime.o readstream.o timer.o ${LIBS}
|
|
||||||
|
|
||||||
ttcpservtime: ttcpservtime.o readstream.o timer.o
|
|
||||||
${CC} ${CCFLAGS} -o $@ ttcpservtime.o readstream.o timer.o ${LIBS}
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f ${PROGS} core core.* *.o temp.* *.out typescript*
|
|
@ -1,26 +0,0 @@
|
|||||||
The files in this archive are the example programs from Chapter 1 of
|
|
||||||
"TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and
|
|
||||||
the UNIX Domain Protocols" by W. Richard Stevens (Addison-Wesley, 1996).
|
|
||||||
|
|
||||||
All the source code files assume tabs stop every 4 columns, not 8. With
|
|
||||||
vi I use ":set tabstop=4".
|
|
||||||
|
|
||||||
The files with "time" in their name are versions that can be used to time
|
|
||||||
the client-server transactions (used for Figure 1.14 of the text). The
|
|
||||||
three server programs with "time" in their name also allow a command-line
|
|
||||||
option specifying how long they should sleep, to simulate the server
|
|
||||||
processing time. Also notice that these "time" clients have a doubly
|
|
||||||
nested loop: one for the request-reply size and another for the number
|
|
||||||
of iterations.
|
|
||||||
|
|
||||||
The two T/TCP files with "big" in their name define the request and reply
|
|
||||||
size to exceed one MSS (used for the example in Section 3.6 of the text).
|
|
||||||
|
|
||||||
*Before* trying to make these programs, do something like
|
|
||||||
|
|
||||||
cc -c error.c sleepus.c
|
|
||||||
ar -crv libmisc.a error.o sleepus.o
|
|
||||||
ranlib libmisc.a # needed for a BSD-derived system
|
|
||||||
|
|
||||||
Then change the definitions of CC, CFLAGS, and LIBS in the Makefile,
|
|
||||||
as appropriate for your system. Then you can "make all".
|
|
@ -1,32 +0,0 @@
|
|||||||
/* Common includes and defines for UDP, TCP, and T/TCP
|
|
||||||
* clients and servers */
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define REQUEST 400 /* max size of request, in bytes */
|
|
||||||
#define REPLY 400 /* max size of reply, in bytes */
|
|
||||||
|
|
||||||
#define UDP_SERV_PORT 7777 /* UDP server's well-known port */
|
|
||||||
#define TCP_SERV_PORT 8888 /* TCP server's well-known port */
|
|
||||||
#define TTCP_SERV_PORT 9999 /* T/TCP server's well-known port */
|
|
||||||
|
|
||||||
/* Following shortens all the type casts of pointer arguments */
|
|
||||||
#define SA struct sockaddr *
|
|
||||||
|
|
||||||
void err_quit(const char *, ...);
|
|
||||||
void err_sys(const char *, ...);
|
|
||||||
int read_stream(int, char *, int);
|
|
||||||
|
|
||||||
/* following for timing versions of client-server */
|
|
||||||
void start_timer(void);
|
|
||||||
double print_timer(void);
|
|
||||||
void sleep_us(unsigned int);
|
|
||||||
|
|
||||||
#define min(a,b) ((a) < (b) ? (a) : (b))
|
|
@ -1,110 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <errno.h> /* for definition of errno */
|
|
||||||
#include <stdarg.h> /* ANSI C header file */
|
|
||||||
#include <stdio.h> /* ANSI C header file */
|
|
||||||
#include <stdlib.h> /* ANSI C header file */
|
|
||||||
#include <string.h> /* ANSI C header file */
|
|
||||||
#include <unistd.h> /* ANSI C header file */
|
|
||||||
|
|
||||||
#define MAXLINE 4096
|
|
||||||
|
|
||||||
static void err_doit(int, const char *, va_list);
|
|
||||||
|
|
||||||
/* Nonfatal error related to a system call.
|
|
||||||
* Print a message and return. */
|
|
||||||
|
|
||||||
void
|
|
||||||
err_ret(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
err_doit(1, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fatal error related to a system call.
|
|
||||||
* Print a message and terminate. */
|
|
||||||
|
|
||||||
void
|
|
||||||
err_sys(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
err_doit(1, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fatal error related to a system call.
|
|
||||||
* Print a message, dump core, and terminate. */
|
|
||||||
|
|
||||||
void
|
|
||||||
err_dump(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
err_doit(1, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
abort(); /* dump core and terminate */
|
|
||||||
exit(1); /* shouldn't get here */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Nonfatal error unrelated to a system call.
|
|
||||||
* Print a message and return. */
|
|
||||||
|
|
||||||
void
|
|
||||||
err_msg(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
err_doit(0, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fatal error unrelated to a system call.
|
|
||||||
* Print a message and terminate. */
|
|
||||||
|
|
||||||
void
|
|
||||||
err_quit(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
err_doit(0, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Print a message and return to caller.
|
|
||||||
* Caller specifies "errnoflag". */
|
|
||||||
|
|
||||||
static void
|
|
||||||
err_doit(int errnoflag, const char *fmt, va_list ap)
|
|
||||||
{
|
|
||||||
int errno_save;
|
|
||||||
char buf[MAXLINE];
|
|
||||||
|
|
||||||
errno_save = errno; /* value caller might want printed */
|
|
||||||
vsprintf(buf, fmt, ap);
|
|
||||||
if (errnoflag)
|
|
||||||
sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
|
|
||||||
strcat(buf, "\n");
|
|
||||||
fflush(stdout); /* in case stdout and stderr are the same */
|
|
||||||
fputs(buf, stderr);
|
|
||||||
fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */
|
|
||||||
return;
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
read_stream(int fd, char *ptr, int maxbytes)
|
|
||||||
{
|
|
||||||
int nleft, nread;
|
|
||||||
|
|
||||||
nleft = maxbytes;
|
|
||||||
while (nleft > 0) {
|
|
||||||
if ( (nread = read(fd, ptr, nleft)) < 0)
|
|
||||||
return(nread); /* error, return < 0 */
|
|
||||||
else if (nread == 0)
|
|
||||||
break; /* EOF, return #bytes read */
|
|
||||||
nleft -= nread;
|
|
||||||
ptr += nread;
|
|
||||||
}
|
|
||||||
return(maxbytes - nleft); /* return >= 0 */
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
void
|
|
||||||
sleep_us(unsigned int nusecs)
|
|
||||||
{
|
|
||||||
struct timeval tval;
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
tval.tv_sec = nusecs / 1000000;
|
|
||||||
tval.tv_usec = nusecs % 1000000;
|
|
||||||
if (select(0, NULL, NULL, NULL, &tval) == 0)
|
|
||||||
break; /* all OK */
|
|
||||||
/*
|
|
||||||
* Note than on an interrupted system call (i.e, SIGIO) there's not
|
|
||||||
* much we can do, since the timeval{} isn't updated with the time
|
|
||||||
* remaining. We could obtain the clock time before the call, and
|
|
||||||
* then obtain the clock time here, subtracting them to determine
|
|
||||||
* how long select() blocked before it was interrupted, but that
|
|
||||||
* seems like too much work :-)
|
|
||||||
*/
|
|
||||||
if (errno == EINTR)
|
|
||||||
continue;
|
|
||||||
err_sys("sleep_us: select error");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[]) /* simple TCP client */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int sockfd, n;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: tcpcli <IP address of server>");
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, 0, sizeof(serv));
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
if (connect(sockfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("connect error");
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
if (write(sockfd, request, REQUEST) != REQUEST)
|
|
||||||
err_sys("write error");
|
|
||||||
if (shutdown(sockfd, 1) < 0)
|
|
||||||
err_sys("shutdown error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, reply, REPLY)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[5000], reply[5000];
|
|
||||||
int sockfd, n, len, i;
|
|
||||||
double rtt, minrtt;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: tcpcli <IP address of server>");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
for (len = 0; len <= 1400; len += 100) {
|
|
||||||
printf("len = %d\n", len);
|
|
||||||
minrtt = 9999999;
|
|
||||||
for (i = 0; i < 30; i++) {
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
start_timer();
|
|
||||||
if (connect(sockfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("connect error");
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
if (write(sockfd, request, len) != len)
|
|
||||||
err_sys("write error");
|
|
||||||
if (shutdown(sockfd, 1) < 0)
|
|
||||||
err_sys("shutdown error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, reply, len)) != len)
|
|
||||||
err_sys("read error, n = %d", n);
|
|
||||||
|
|
||||||
rtt = print_timer();
|
|
||||||
minrtt = min(minrtt, rtt);
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
printf("min rtt = %9.3f\n\n", minrtt);
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main() /* simple TCP server */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int listenfd, sockfd, n, clilen;
|
|
||||||
|
|
||||||
if ( (listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, 0, sizeof(serv));
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
if (listen(listenfd, SOMAXCONN) < 0)
|
|
||||||
err_sys("listen error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("accept error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, request, REQUEST)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] and create reply[] ... */
|
|
||||||
|
|
||||||
if (write(sockfd, reply, REPLY) != REPLY)
|
|
||||||
err_sys("write error");
|
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[5000], reply[5000];
|
|
||||||
int listenfd, sockfd, n, clilen, spt;
|
|
||||||
|
|
||||||
if (argc > 1)
|
|
||||||
spt = atoi(argv[1]); /* optional server processing time, in ms */
|
|
||||||
else
|
|
||||||
spt = 0;
|
|
||||||
|
|
||||||
if ( (listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
if (listen(listenfd, SOMAXCONN) < 0)
|
|
||||||
err_sys("listen error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("accept error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, request, sizeof(request))) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] ... */
|
|
||||||
if (spt)
|
|
||||||
sleep_us(spt * 1000);
|
|
||||||
|
|
||||||
if (write(sockfd, reply, n) != n)
|
|
||||||
err_sys("write error");
|
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
void tvsub(struct timeval *, struct timeval *);
|
|
||||||
|
|
||||||
struct timeval tvstart, tvend;
|
|
||||||
|
|
||||||
void
|
|
||||||
start_timer()
|
|
||||||
{
|
|
||||||
if (gettimeofday(&tvstart, NULL) < 0)
|
|
||||||
err_sys("start_timer: gettimeofday error");
|
|
||||||
}
|
|
||||||
|
|
||||||
double
|
|
||||||
print_timer()
|
|
||||||
{
|
|
||||||
double triptime;
|
|
||||||
|
|
||||||
if (gettimeofday(&tvend, NULL) < 0)
|
|
||||||
err_sys("print_timer: gettimeofday error");
|
|
||||||
|
|
||||||
tvsub(&tvend, &tvstart);
|
|
||||||
triptime = ((double) tvend.tv_sec) * 1000.0 +
|
|
||||||
((double) tvend.tv_usec) / 1000.0; /* in millisec */
|
|
||||||
printf("rtt = %9.3f ms\n", triptime);
|
|
||||||
return(triptime);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Subtract two timeval structures: *end -= *start */
|
|
||||||
|
|
||||||
void
|
|
||||||
tvsub(struct timeval *end, struct timeval *start)
|
|
||||||
{
|
|
||||||
if ((end->tv_usec -= start->tv_usec) < 0) {
|
|
||||||
--end->tv_sec;
|
|
||||||
end->tv_usec += 1000000;
|
|
||||||
}
|
|
||||||
end->tv_sec -= start->tv_sec;
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[]) /* T/TCP client */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int sockfd, n;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: ttcpcli <IP address of server>");
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, 0, sizeof(serv));
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
if (sendto(sockfd, request, REQUEST, MSG_EOF,
|
|
||||||
(SA) &serv, sizeof(serv)) != REQUEST)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, reply, REPLY)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
#include <netinet/tcp.h>
|
|
||||||
|
|
||||||
#undef REQUEST
|
|
||||||
#undef REPLY
|
|
||||||
|
|
||||||
#define REQUEST 3300 /* max size of request, in bytes */
|
|
||||||
#define REPLY 3400 /* max size of reply, in bytes */
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int sockfd, n;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: ttcpcli <IP address of server>");
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
n = 1;
|
|
||||||
if (setsockopt(sockfd, SOL_SOCKET, SO_DEBUG,
|
|
||||||
(char *) &n, sizeof(n)) < 0)
|
|
||||||
err_sys("SO_DEBUG error");
|
|
||||||
|
|
||||||
#ifdef notdef
|
|
||||||
n = 1;
|
|
||||||
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NOPUSH,
|
|
||||||
(char *) &n, sizeof(n)) < 0)
|
|
||||||
err_sys("TCP_NOPUSH error");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
if (sendto(sockfd, request, REQUEST, MSG_EOF,
|
|
||||||
(SA) &serv, sizeof(serv)) != REQUEST)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, reply, REPLY)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[5000], reply[5000];
|
|
||||||
int sockfd, n, len, i;
|
|
||||||
double rtt, minrtt;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: ttcpcli <IP address of server>");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(TTCP_SERV_PORT);
|
|
||||||
|
|
||||||
for (len = 0; len <= 1400; len += 100) {
|
|
||||||
printf("len = %d\n", len);
|
|
||||||
minrtt = 9999999;
|
|
||||||
for (i = 0; i < 30; i++) {
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
start_timer();
|
|
||||||
if (sendto(sockfd, request, len, MSG_EOF,
|
|
||||||
(SA) &serv, sizeof(serv)) != len)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, reply, len)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
rtt = print_timer();
|
|
||||||
minrtt = min(minrtt, rtt);
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
printf("min rtt = %9.3f\n\n", minrtt);
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[]) /* T/TCP client */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char reply[REPLY];
|
|
||||||
static char request[] = "GET / HTTP/1.0\n\r\n\r";
|
|
||||||
int sockfd, n;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: ttcpcli <IP address of server>");
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(80);
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
if (sendto(sockfd, request, sizeof(request)-1, MSG_EOF,
|
|
||||||
(SA) &serv, sizeof(serv)) != sizeof(request)-1)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
while ( (n = read_stream(sockfd, reply, REPLY)) > 0) {
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
write(1, reply, n);
|
|
||||||
}
|
|
||||||
if (n < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main() /* T/TCP server */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int listenfd, sockfd, n, clilen;
|
|
||||||
|
|
||||||
if ( (listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, 0, sizeof(serv));
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
if (listen(listenfd, SOMAXCONN) < 0)
|
|
||||||
err_sys("listen error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("accept error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, request, REQUEST)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] and create reply[] ... */
|
|
||||||
|
|
||||||
if (send(sockfd, reply, REPLY, MSG_EOF) != REPLY)
|
|
||||||
err_sys("send error");
|
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
#undef REQUEST
|
|
||||||
#undef REPLY
|
|
||||||
|
|
||||||
#define REQUEST 3300 /* max size of request, in bytes */
|
|
||||||
#define REPLY 3400 /* max size of reply, in bytes */
|
|
||||||
|
|
||||||
int
|
|
||||||
main()
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int listenfd, sockfd, n, clilen;
|
|
||||||
|
|
||||||
if ( (listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(TCP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
if (listen(listenfd, SOMAXCONN) < 0)
|
|
||||||
err_sys("listen error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("accept error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, request, REQUEST)) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] ... */
|
|
||||||
|
|
||||||
if (sendto(sockfd, reply, REPLY, MSG_EOF, (SA) NULL, 0) != REPLY)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[5000], reply[5000];
|
|
||||||
int listenfd, sockfd, n, clilen, spt;
|
|
||||||
|
|
||||||
if (argc > 1)
|
|
||||||
spt = atoi(argv[1]); /* optional server processing time, in ms */
|
|
||||||
else
|
|
||||||
spt = 0;
|
|
||||||
|
|
||||||
if ( (listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(TTCP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
if (listen(listenfd, SOMAXCONN) < 0)
|
|
||||||
err_sys("listen error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("accept error");
|
|
||||||
|
|
||||||
if ( (n = read_stream(sockfd, request, sizeof(request))) < 0)
|
|
||||||
err_sys("read error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] ... */
|
|
||||||
if (spt)
|
|
||||||
sleep_us(spt * 1000);
|
|
||||||
|
|
||||||
if (sendto(sockfd, reply, n, MSG_EOF, (SA) NULL, 0) != n)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[]) /* simple UDP client */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int sockfd, n;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: udpcli <IP address of server>");
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, 0, sizeof(serv));
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(UDP_SERV_PORT);
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
if (sendto(sockfd, request, REQUEST, 0,
|
|
||||||
(SA) &serv, sizeof(serv)) != REQUEST)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
if ( (n = recvfrom(sockfd, reply, REPLY, 0,
|
|
||||||
(SA) NULL, (int *) NULL)) < 0)
|
|
||||||
err_sys("recvfrom error");
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv;
|
|
||||||
char request[5000], reply[5000];
|
|
||||||
int sockfd, n, len, i;
|
|
||||||
double rtt, minrtt;
|
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
err_quit("usage: udpcli <IP address of server>");
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = inet_addr(argv[1]);
|
|
||||||
serv.sin_port = htons(UDP_SERV_PORT);
|
|
||||||
|
|
||||||
/* form request[] ... */
|
|
||||||
|
|
||||||
for (len = 0; len <= 1400; len += 100) { /* len = amount of user data */
|
|
||||||
printf("len = %d\n", len);
|
|
||||||
minrtt = 9999999;
|
|
||||||
for (i = 0; i < 30; i++) {
|
|
||||||
start_timer();
|
|
||||||
|
|
||||||
if (sendto(sockfd, request, len, 0,
|
|
||||||
(SA) &serv, sizeof(serv)) != len)
|
|
||||||
err_sys("sendto error");
|
|
||||||
|
|
||||||
if ( (n = recvfrom(sockfd, reply, sizeof(reply), 0,
|
|
||||||
(SA) NULL, (int *) NULL)) != len)
|
|
||||||
err_sys("recvfrom error");
|
|
||||||
|
|
||||||
rtt = print_timer();
|
|
||||||
minrtt = min(minrtt, rtt);
|
|
||||||
|
|
||||||
/* process "n" bytes of reply[] ... */
|
|
||||||
}
|
|
||||||
printf("min rtt = %9.3f\n\n", minrtt);
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main() /* simple UDP server */
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[REQUEST], reply[REPLY];
|
|
||||||
int sockfd, n, clilen;
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, 0, sizeof(serv));
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(UDP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(sockfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (n = recvfrom(sockfd, request, REQUEST, 0,
|
|
||||||
(SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("recvfrom error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] and create reply[] ... */
|
|
||||||
|
|
||||||
if (sendto(sockfd, reply, REPLY, 0,
|
|
||||||
(SA) &cli, sizeof(cli)) != REPLY)
|
|
||||||
err_sys("sendto error");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1996 W. Richard Stevens. All rights reserved.
|
|
||||||
* Permission to use or modify this software and its documentation only for
|
|
||||||
* educational purposes and without fee is hereby granted, provided that
|
|
||||||
* the above copyright notice appear in all copies. The author makes no
|
|
||||||
* representations about the suitability of this software for any purpose.
|
|
||||||
* It is provided "as is" without express or implied warranty.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cliserv.h"
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
struct sockaddr_in serv, cli;
|
|
||||||
char request[5000], reply[5000];
|
|
||||||
int sockfd, n, clilen, spt;
|
|
||||||
|
|
||||||
if (argc > 1)
|
|
||||||
spt = atoi(argv[1]); /* optional server processing time, in ms */
|
|
||||||
else
|
|
||||||
spt = 0;
|
|
||||||
|
|
||||||
if ( (sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
|
|
||||||
err_sys("socket error");
|
|
||||||
|
|
||||||
memset(&serv, sizeof(serv), 0);
|
|
||||||
serv.sin_family = AF_INET;
|
|
||||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
serv.sin_port = htons(UDP_SERV_PORT);
|
|
||||||
|
|
||||||
if (bind(sockfd, (SA) &serv, sizeof(serv)) < 0)
|
|
||||||
err_sys("bind error");
|
|
||||||
|
|
||||||
for ( ; ; ) {
|
|
||||||
clilen = sizeof(cli);
|
|
||||||
if ( (n = recvfrom(sockfd, request, sizeof(request), 0,
|
|
||||||
(SA) &cli, &clilen)) < 0)
|
|
||||||
err_sys("recvfrom error");
|
|
||||||
|
|
||||||
/* process "n" bytes of request[] ... */
|
|
||||||
if (spt)
|
|
||||||
sleep_us(spt * 1000);
|
|
||||||
|
|
||||||
if (sendto(sockfd, reply, n, 0,
|
|
||||||
(SA) &cli, sizeof(cli)) != n)
|
|
||||||
err_sys("sendto error");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue