/* -*- c-basic-offset: 8; -*-
*
* Copyright (c) 1993 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 <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <string.h>
#include "sock.h"
char *host; /* hostname or dotted-decimal string */
char *port;
/* DefinE global variables */
int bindport; /* 0 or TCP or UDP port number to bind */
/* set by -b or -l options */
int broadcast; /* SO_BROADCAST */
int cbreak; /* set terminal to cbreak mode */
int chunkwrite; /* write in small chunks; not all-at-once */
int client = 1; /* acting as client is the default */
int connectudp = 1; /* connect UDP client */
int crlf; /* convert newline to CR/LF & vice versa */
int debug; /* SO_DEBUG */
int dofork; /* concurrent server, do a fork() */
int dontroute; /* SO_DONTROUTE */
char foreignip[32]; /* foreign IP address, dotted-decimal string */
int foreignport; /* foreign port number */
int halfclose; /* TCP half close option */
int ignorewerr; /* true if write() errors should be ignored */
int iptos = -1; /* IP_TOS opton */
int ipttl = -1; /* IP_TTL opton */
char joinip[32]; /* multicast IP address, dotted-decimal string */
int keepalive; /* SO_KEEPALIVE */
long linger = -1; /* 0 or positive turns on option */
int listenq = 5; /* listen queue for TCP Server */
char localip[32]; /* local IP address, dotted-decimal string */
int maxseg; /* TCP_MAXSEG */
int mcastttl; /* multicast TTL */
int msgpeek; /* MSG_PEEK */
int nodelay; /* TCP_NODELAY (Nagle algorithm) */
int nbuf = 1024; /* number of buffers to write (sink mode) */
int onesbcast; /* set IP_ONESBCAST for 255.255.255.255 bcasts */
int pauseclose; /* #ms to sleep after recv FIN, before close */
int pauseinit; /* #ms to sleep before first read */
int pauselisten; /* #ms to sleep after listen() */
int pauserw; /* #ms to sleep before each read or write */
int reuseaddr; /* SO_REUSEADDR */
int reuseport; /* SO_REUSEPORT */
int readlen = 1024; /* default read length for socket */
int writelen = 1024; /* default write length for socket */
int recvdstaddr; /* IP_RECVDSTADDR option */
int rcvbuflen; /* size for SO_RCVBUF */
int sndbuflen; /* size for SO_SNDBUF */
long rcvtimeo; /* SO_RCVTIMEO */
long sndtimeo; /* SO_SNDTIMEO */
int sroute_cnt; /* count of #IP addresses in route */
char *rbuf; /* pointer that is malloc'ed */
char *wbuf; /* pointer that is malloc'ed */
int server; /* to act as server requires -s option */
int sigio; /* send SIGIO */
int sourcesink; /* source/sink mode */
int udp; /* use UDP instead of TCP */
int urgwrite; /* write urgent byte after this write */
int verbose; /* each -v increments this by 1 */
int usewritev; /* use writev() instead of write() */
struct sockaddr_in cliaddr, servaddr;
static void usage(const char *);
int
main(int argc, char *argv[])
{
int c, fd;
char *ptr;
if (argc < 2)
usage("");
opterr = 0; /* don't want getopt() writing to stderr */
while ( (c = getopt(argc, argv, "2b:cf:g:hij:kl:n:op:q:r:st:uvw:x:y:ABCDEFG:H:IJ:KL:NO:P:Q:R:S:TU:VWX:YZ")) != -1) {
switch (c) {
#ifdef IP_ONESBCAST
case '2': /* use 255.255.255.255 as broadcast address */
onesbcast = 1;
break;
#endif
case 'b':
bindport = atoi(optarg);
break;
case 'c': /* convert newline to CR/LF & vice versa */
crlf = 1;
break;
case 'f': /* foreign IP address and port#: a.b.c.d.p */
if ( (ptr = strrchr(optarg, '.')) == NULL)
usage("invalid -f option");
*ptr++ = 0; /* null replaces final period */
foreignport = atoi(ptr); /* port number */
strcpy(foreignip, optarg); /* save dotted-decimal IP */
break;
case 'g': /* loose source route */
sroute_doopt(0, optarg);
break;
case 'h': /* TCP half-close option */
halfclose = 1;
break;
case 'i': /* source/sink option */
sourcesink = 1;
break;
#ifdef IP_ADD_MEMBERSHIP
case 'j': /* join multicast group a.b.c.d */
strcpy(joinip, optarg); /* save dotted-decimal IP */
break;
#endif
case 'k': /* chunk-write option */
chunkwrite = 1;
break;
case 'l': /* local IP address and port#: a.b.c.d.p */
if ( (ptr = strrchr(optarg, '.')) == NULL)
usage("invalid -l option");
*ptr++ = 0; /* null replaces final period */
bindport = atoi(ptr); /* port number */
strcpy(localip, optarg); /* save dotted-decimal IP */
break;
case 'n': /* number of buffers to write */
nbuf = atol(optarg);
break;
case 'o': /* do not connect UDP client */
connectudp = 0;
break;
case 'p': /* pause before each read or write */
pauserw = atoi(optarg);
break;
case 'q': /* listen queue for TCP server */
listenq = atoi(optarg);
break;
case 'r': /* read() length */
readlen = atoi(optarg);
break;
case 's': /* server */
server = 1;
client = 0;
break;
#ifdef IP_MULTICAST_TTL
case 't': /* IP_MULTICAST_TTL */
mcastttl = atoi(optarg);
break;
#endif
case 'u': /* use UDP instead of TCP */
udp = 1;
break;
case 'v': /* output what's going on */
verbose++;
break;
case 'w': /* write() length */
writelen = atoi(optarg);
break;
case 'x': /* SO_RCVTIMEO socket option */
rcvtimeo = atol(optarg);
break;
case 'y': /* SO_SNDTIMEO socket option */
sndtimeo = atol(optarg);
break;
case 'A': /* SO_REUSEADDR socket option */
reuseaddr = 1;
break;
case 'B': /* SO_BROADCAST socket option */
broadcast = 1;
break;
case 'C': /* set standard input to cbreak mode */
cbreak = 1;
break;
case 'D': /* SO_DEBUG socket option */
debug = 1;
break;
case 'E': /* IP_RECVDSTADDR socket option */
recvdstaddr = 1;
break;
case 'F': /* concurrent server, do a fork() */
dofork = 1;
break;
case 'G': /* strict source route */
sroute_doopt(1, optarg);
break;
#ifdef IP_TOS
case 'H': /* IP_TOS socket option */
iptos = atoi(optarg);
break;
#endif
case 'I': /* SIGIO signal */
sigio = 1;
break;
#ifdef IP_TTL
case 'J': /* IP_TTL socket option */
ipttl = atoi(optarg);
break;
#endif
case 'K': /* SO_KEEPALIVE socket option */
keepalive = 1;
break;
case 'L': /* SO_LINGER socket option */
linger = atol(optarg);
break;
case 'N': /* SO_NODELAY socket option */
nodelay = 1;
break;
case 'O': /* pause before listen(), before first accept() */
pauselisten = atoi(optarg);
break;
case 'P': /* pause before first read() */
pauseinit = atoi(optarg);
break;
case 'Q': /* pause after receiving FIN, but before close() */
pauseclose = atoi(optarg);
break;
case 'R': /* SO_RCVBUF socket option */
rcvbuflen = atoi(optarg);
break;
case 'S': /* SO_SNDBUF socket option */
sndbuflen = atoi(optarg);
break;
#ifdef SO_REUSEPORT
case 'T': /* SO_REUSEPORT socket option */
reuseport = 1;
break;
#endif
case 'U': /* when to write urgent byte */
urgwrite = atoi(optarg);
break;
case 'V': /* use writev() instead of write() */
usewritev = 1;
chunkwrite = 1; /* implies this option too */
break;
case 'W': /* ignore write errors */
ignorewerr = 1;
break;
case 'X': /* TCP maximum segment size option */
maxseg = atoi(optarg);
break;
case 'Y': /* SO_DONTROUTE socket option */
dontroute = 1;
break;
case 'Z': /* MSG_PEEK option */
没有合适的资源?快使用搜索试试~ 我知道了~
TCPIP详解SOCK程序源码
3星 · 超过75%的资源 需积分: 16 48 下载量 157 浏览量
2010-03-02
23:40:10
上传
评论
收藏 107KB GZ 举报
温馨提示
共50个文件
c:20个
h:5个
in:4个
TCPIP详解SOCK程序源码 TCPIP详解SOCK程序源码 TCPIP详解SOCK程序源码
资源推荐
资源详情
资源评论
收起资源包目录
sock-0.3.1.tar.gz (50个子文件)
sock-0.3.1
missing 6KB
COPYING 406B
aclocal.m4 32KB
INSTALL 9KB
config.h.in 5KB
mkinstalldirs 726B
configure 184KB
acinclude.m4 3KB
ChangeLog 779B
install-sh 5KB
sock.spec 1KB
Makefile.in 18KB
TODO 0B
depcomp 16KB
configure.in 3KB
AUTHORS 174B
src
sourceudp.c 2KB
tellwait.c 1KB
main.c 12KB
buffers.c 2KB
sourcetcp.c 2KB
multicast.c 1KB
loopudp.c 6KB
addrinfo.h 2KB
writen.c 629B
servopen.c 4KB
sock.h 4KB
sockopts.c 10KB
Makefile.in 14KB
sourceroute.c 4KB
TODO 201B
ourhdr.h 4KB
write.c 2KB
looptcp.c 3KB
error.c 2KB
crlf.c 1KB
sinktcp.c 2KB
global.h 923B
Makefile.am 335B
sleepus.c 819B
pattern.c 685B
cliopen.c 4KB
sinkudp.c 2KB
autogen.sh 2KB
config.guess 20KB
README 747B
config.sub 19KB
NEWS 22B
acconfig.h 1KB
Makefile.am 66B
共 50 条
- 1
资源评论
- bssyxxlw2011-11-15这叫详解??真会骗人,不信的自己看,就是一些源码
- brokening2014-02-15对初学者还是有点参考价值
newtoncau
- 粉丝: 5
- 资源: 188
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功