📄 00000007.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: emi (你既无心我便休), 信区: Linux <BR>标 题: 改进版本的datapipe.c <BR>发信站: BBS 水木清华站 (Thu Feb 10 16:54:05 2000) <BR> <BR>呵呵,从网上抄的。很好用。 <BR>/* <BR> * Datapipe - Create a listen socket to pipe connections to another <BR> * machine/port. 'localport' accepts connections on the machine running <BR> * datapipe, which will connect to 'remoteport' on 'remotehost'. <BR> * It will fork itself into the background on non-Windows machines. <BR> * <BR> * This implementation of the traditional "datapipe" does not depend on <BR> * forking to handle multiple simultaneous clients, and instead is able <BR> * to do all processing from within a single process, making it ideal <BR> * for low-memory environments. The elimination of the fork also <BR> * allows it to be used in environments without fork, such as Win32. <BR> * <BR> * This implementation also differs from most others in that it allows <BR> * the specific IP address of the interface to listen on to be specified. <BR> * This is useful for machines that have multiple IP addresses. The <BR> * specified listening address will also be used for making the outgoing <BR> * connections on. <BR> * <BR> * Note that select() is not used to perform writability testing on the <BR> * outgoing sockets, so conceivably other connections might have delayed <BR> * responses if any of the connected clients or the connection to the <BR> * target machine is slow enough to allow its outgoing buffer to fill <BR> * to capacity. <BR> * <BR> * Compile with: <BR> * cc -O -o datapipe datapipe.c <BR> * On Solaris/SunOS, compile with: <BR> * gcc -Wall datapipe.c -lsocket -lnsl -o datapipe <BR> * On Windows compile with: <BR> * bcc32 /w datapipe.c (Borland C++) <BR> * cl /W3 datapipe.c wsock32.lib (Microsoft Visual C++) <BR> * <BR> * Run as: <BR> * datapipe localhost localport remoteport remotehost <BR> * <BR> * <BR> * written by Jeff Lawson <<A HREF="mailto:jlawson@bovine.net>">jlawson@bovine.net></A> <BR> * inspired by code originally by Todd Vierling, 1995. <BR> */ <BR> <BR> <BR>#include <stdio.h> <BR>#include <stdlib.h> <BR>#include <string.h> <BR>#include <errno.h> <BR>#include <time.h> <BR>#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) <BR> #define WIN32_LEAN_AND_MEAN <BR> #include <winsock.h> <BR> #define bzero(p, l) memset(p, 0, l) <BR> #define bcopy(s, t, l) memmove(t, s, l) <BR>#else <BR> #include <sys/time.h> <BR> #include <sys/types.h> <BR> #include <sys/socket.h> <BR> #include <sys/wait.h> <BR> #include <netinet/in.h> <BR> #include <arpa/inet.h> <BR> #include <unistd.h> <BR> #include <netdb.h> <BR> #include <strings.h> <BR> #define recv(x,y,z,a) read(x,y,z) <BR> #define send(x,y,z,a) write(x,y,z) <BR> #define closesocket(s) close(s) <BR> typedef int SOCKET; <BR>#endif <BR> <BR>#ifndef INADDR_NONE <BR>#define INADDR_NONE 0xffffffff <BR>#endif <BR> <BR> <BR>struct client_t <BR>{ <BR> int inuse; <BR> SOCKET csock, osock; <BR> time_t activity; <BR>}; <BR> <BR>#define MAXCLIENTS 40 <BR>#define IDLETIMEOUT 400 <BR> <BR> <BR>const char ident[] = "$Id: datapipe.c,v 1.8 1999/01/29 01:21:54 jlawson Exp $"; <BR> <BR>int main(int argc, char *argv[]) <BR>{ <BR> SOCKET lsock; <BR> char buf[4096]; <BR> struct sockaddr_in laddr, oaddr; <BR> int i; <BR> struct client_t clients[MAXCLIENTS]; <BR> <BR> <BR>#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) <BR> /* Winsock needs additional startup activities */ <BR> WSADATA wsadata; <BR> WSAStartup(MAKEWORD(1,1), &wsadata); <BR>#endif <BR> <BR> <BR> /* check number of command line arguments */ <BR> if (argc != 5) { <BR> fprintf(stderr,"Usage: %s localhost localport remotehost remoteport\n",argv[0]); <BR> return 30; <BR> } <BR> <BR> <BR> /* reset all of the client structures */ <BR> for (i = 0; i < MAXCLIENTS; i++) <BR> clients[i].inuse = 0; <BR> <BR> <BR> /* determine the listener address and port */ <BR> bzero(&laddr, sizeof(struct sockaddr_in)); <BR> laddr.sin_family = AF_INET; <BR> laddr.sin_port = htons((unsigned short) atol(argv[2])); <BR> laddr.sin_addr.s_addr = inet_addr(argv[1]); <BR> if (!laddr.sin_port) { <BR> fprintf(stderr, "invalid listener port\n"); <BR> return 20; <BR> } <BR> if (laddr.sin_addr.s_addr == INADDR_NONE) { <BR> struct hostent *n; <BR> if ((n = gethostbyname(argv[1])) == NULL) { <BR> perror("gethostbyname"); <BR> return 20; <BR> } <BR> bcopy(n->h_addr, (char *) &laddr.sin_addr, n->h_length); <BR> } <BR> <BR> <BR> /* determine the outgoing address and port */ <BR> bzero(&oaddr, sizeof(struct sockaddr_in)); <BR> oaddr.sin_family = AF_INET; <BR> oaddr.sin_port = htons((unsigned short) atol(argv[4])); <BR> if (!oaddr.sin_port) { <BR> fprintf(stderr, "invalid target port\n"); <BR> return 25; <BR> } <BR> oaddr.sin_addr.s_addr = inet_addr(argv[3]); <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -