分类:
2006-08-01 17:20:48
/*----------------------源代码开始--------------------------------------------*/ #include#include #include #include #include #include #include #include /********************************************************************* *filename: cycletcpserver.c *purpose: 循环tcp服务端程序 *tidied by: zhoulifa(zhoulifa@163.com) 周立发() Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言 *date time:2006-07-04 22:00:00 *Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途 * 但请遵循GPL *Thanks to: Google.com *********************************************************************/ int main(int argc, char ** argv) { int sockfd,new_fd; /* 监听socket: sock_fd,数据传输socket: new_fd */ struct sockaddr_in my_addr; /* 本机地址信息 */ struct sockaddr_in their_addr; /* 客户地址信息 */ unsigned int sin_size, myport, lisnum; if(argv[1]) myport = atoi(argv[1]); else myport = 7838; if(argv[2]) lisnum = atoi(argv[2]); else lisnum = 2; if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family=PF_INET; my_addr.sin_port=htons(myport); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero), 0); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } if (listen(sockfd, lisnum) == -1) { perror("listen"); exit(1); } while(1) { sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("accept"); continue; } printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr)); if (!fork()) { /* 子进程代码段 */ if (send(new_fd, "Hello, world!\n", 14, 0) == -1) { perror("send"); close(new_fd); exit(0); } } close(new_fd); /*父进程不再需要该socket*/ waitpid(-1,NULL,WNOHANG);/*等待子进程结束,清除子进程所占用资源*/ } } /*----------------------源代码结束--------------------------------------------*/
/*----------------------源代码开始--------------------------------------------*/ #include#include #include #include #include #include #include #include #define MAXDATASIZE 100 /*每次最大数据传输量 */ /********************************************************************* *filename: cycletcpclient.c *purpose: 循环tcp客户端程序 *tidied by: zhoulifa(zhoulifa@163.com) 周立发() Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言 *date time:2006-07-04 22:20:00 *Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途 * 但请遵循GPL *Thanks to: Google.com *Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力 *********************************************************************/ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in their_addr; unsigned int myport; if(argv[2]) myport = atoi(argv[2]); else myport = 7838; if (argc != 3) { fprintf(stderr,"usage: %s hostname port\n", argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { herror("gethostbyname"); exit(1); } if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family=PF_INET; their_addr.sin_port=htons(myport); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero),0); if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = 0; printf("Received: %s\n",buf); close(sockfd); return 0; } /*----------------------源代码结束--------------------------------------------*/
administrator@ubuzlf:/data/example/c$ ./tcpserver server: got connection from 127.0.0.1 server: got connection from 127.0.0.1 server: got connection from 127.0.0.1 |
administrator@ubuzlf:/data/example/c$ ./tcpclient 127.0.0.1 7838 Received: Hello, world! administrator@ubuzlf:/data/example/c$ ./tcpclient 127.0.0.1 7838 Received: Hello, world! administrator@ubuzlf:/data/example/c$ ./tcpclient 127.0.0.1 7838 Received: Hello, world! |
/*----------------------源代码开始--------------------------------------------*/ #include#include #include #include #include #include #include #include #include #include #include static int forward_port; #undef max #define max(x,y) ((x) > (y) ? (x) : (y)) /*************************关于本文档************************************ *filename: tcpforwardport.c *purpose: 演示了select的用法,这是一个极好的代理软件核心,专门作端口映射用 *tidied by: zhoulifa(zhoulifa@163.com) 周立发() Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言 *date time:2006-07-05 19:00:00 *Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途 * 但请遵循GPL *Thanks to: Paul Sheer 感谢Paul Sheer在select_tut的man手册里提供了这份源代码 *Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力 *********************************************************************/ static int listen_socket (int listen_port) { struct sockaddr_in a; int s; int yes; if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) { perror ("socket"); return -1; } yes = 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof (yes)) < 0) { perror ("setsockopt"); close (s); return -1; } memset (&a, 0, sizeof (a)); a.sin_port = htons (listen_port); a.sin_family = AF_INET; if (bind(s, (struct sockaddr *) &a, sizeof (a)) < 0) { perror ("bind"); close (s); return -1; } printf ("accepting connections on port %d\n", (int) listen_port); listen (s, 10); return s; } static int connect_socket (int connect_port, char *address) { struct sockaddr_in a; int s; if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) { perror ("socket"); close (s); return -1; } memset (&a, 0, sizeof (a)); a.sin_port = htons (connect_port); a.sin_family = AF_INET; if (!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)) { perror ("bad IP address format"); close (s); return -1; } if (connect(s, (struct sockaddr *) &a, sizeof (a)) < 0) { perror ("connect()"); shutdown (s, SHUT_RDWR); close (s); return -1; } return s; } #define SHUT_FD1 { \ if (fd1 >= 0) { \ shutdown (fd1, SHUT_RDWR); \ close (fd1); \ fd1 = -1; \ } \ } #define SHUT_FD2 { \ if (fd2 >= 0) { \ shutdown (fd2, SHUT_RDWR); \ close (fd2); \ fd2 = -1; \ } \ } #define BUF_SIZE 1024 int main (int argc, char **argv) { int h; int fd1 = -1, fd2 = -1; char buf1[BUF_SIZE], buf2[BUF_SIZE]; int buf1_avail, buf1_written; int buf2_avail, buf2_written; if (argc != 4) { fprintf (stderr, "Usage\n\tfwd \n"); exit (1); } signal (SIGPIPE, SIG_IGN); forward_port = atoi (argv[2]); /*建立监听socket*/ h = listen_socket (atoi (argv[1])); if (h < 0) exit (1); for (;;) { int r, nfds = 0; fd_set rd, wr, er; FD_ZERO (&rd); FD_ZERO (&wr); FD_ZERO (&er); FD_SET (h, &rd); /*把监听socket和可读socket三个一起放入select的可读句柄列表里*/ nfds = max (nfds, h); if (fd1 > 0 && buf1_avail < BUF_SIZE) { FD_SET (fd1, &rd); nfds = max (nfds, fd1); } if (fd2 > 0 && buf2_avail < BUF_SIZE) { FD_SET (fd2, &rd); nfds = max (nfds, fd2); } /*把可写socket两个一起放入select的可写句柄列表里*/ if (fd1 > 0 && buf2_avail - buf2_written > 0) { FD_SET (fd1, &wr); nfds = max (nfds, fd1); } if (fd2 > 0 && buf1_avail - buf1_written > 0) { FD_SET (fd2, &wr); nfds = max (nfds, fd2); } /*把有异常数据的socket两个一起放入select的异常句柄列表里*/ if (fd1 > 0) { FD_SET (fd1, &er); nfds = max (nfds, fd1); } if (fd2 > 0) { FD_SET (fd2, &er); nfds = max (nfds, fd2); } /*开始select*/ r = select (nfds + 1, &rd, &wr, &er, NULL); if (r == -1 && errno == EINTR) continue; if (r < 0) { perror ("select()"); exit (1); } /*处理新连接*/ if (FD_ISSET (h, &rd)) { unsigned int l; struct sockaddr_in client_address; memset (&client_address, 0, l = sizeof (client_address)); r = accept (h, (struct sockaddr *)&client_address, &l); if (r < 0) { perror ("accept()"); } else { /*关闭原有连接,把新连接作为fd1,同时连接新的目标fd2*/ SHUT_FD1; SHUT_FD2; buf1_avail = buf1_written = 0; buf2_avail = buf2_written = 0; fd1 = r; fd2 = connect_socket (forward_port, argv[3]); if (fd2 < 0) { SHUT_FD1; } else printf ("connect from %s\n", inet_ntoa(client_address.sin_addr)); } } /* NB: read oob data before normal reads */ if (fd1 > 0) if (FD_ISSET (fd1, &er)) { char c; errno = 0; r = recv (fd1, &c, 1, MSG_OOB); if (r < 1) { SHUT_FD1; } else send (fd2, &c, 1, MSG_OOB); } if (fd2 > 0) if (FD_ISSET (fd2, &er)) { char c; errno = 0; r = recv (fd2, &c, 1, MSG_OOB); if (r < 1) { SHUT_FD1; } else send (fd1, &c, 1, MSG_OOB); } /* NB: read data from fd1 */ if (fd1 > 0) if (FD_ISSET (fd1, &rd)) { r = read (fd1, buf1 + buf1_avail, BUF_SIZE - buf1_avail); if (r < 1) { SHUT_FD1; } else buf1_avail += r; } /* NB: read data from fd2 */ if (fd2 > 0) if (FD_ISSET (fd2, &rd)) { r = read (fd2, buf2 + buf2_avail, BUF_SIZE - buf2_avail); if (r < 1) { SHUT_FD2; } else buf2_avail += r; } /* NB: write data to fd1 */ if (fd1 > 0) if (FD_ISSET (fd1, &wr)) { r = write (fd1, buf2 + buf2_written, buf2_avail - buf2_written); if (r < 1) { SHUT_FD1; } else buf2_written += r; } /* NB: write data to fd1 */ if (fd2 > 0) if (FD_ISSET (fd2, &wr)) { r = write (fd2, buf1 + buf1_written, buf1_avail - buf1_written); if (r < 1) { SHUT_FD2; } else buf1_written += r; } /* check if write data has caught read data */ if (buf1_written == buf1_avail) buf1_written = buf1_avail = 0; if (buf2_written == buf2_avail) buf2_written = buf2_avail = 0; /* one side has closed the connection, keep writing to the other side until empty */ if (fd1 < 0 && buf1_avail - buf1_written == 0) { SHUT_FD2; } if (fd2 < 0 && buf2_avail - buf2_written == 0) { SHUT_FD1; } } return 0; } /*----------------------源代码结束--------------------------------------------*/