新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > 开发语言 > Perl教程 > 正文:网络socket编程指南

网络socket编程指南

新客网 XKER.COM 2008-04-18 来源:chinaunix 流浪者 收藏本文
服务器代码: 
#include <stdio.h>;
  #include <stdlib.h>;
  #include <errno.h>;
  #include <string.h>;
  #include <sys/types.h>;
  #include <netinet/in.h>;
  #include <sys/socket.h>;
  #include <sys/wait.h>;
#define MYPORT 3490 /*定义用户连接端口*/ 
#define BACKLOG 10 /*多少等待连接控制*/ 
main() 
   { 
   int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd 
*/ 
   struct sockaddr_in my_addr; /* my address information */ 
   struct sockaddr_in their_addr; /* connector's address information */ 
   int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
   perror("socket"); 
   exit(1); 
   } 

my_addr.sin_family = AF_INET; /* host byte order */ 
   my_addr.sin_port = htons(MYPORT); /* short, network byte order */ 
   my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ 
   bzero(&amp;(my_addr.sin_zero),; /* zero the rest of the struct */ 

if (bind(sockfd, (struct sockaddr *)&amp;my_addr, sizeof(struct 
sockaddr))== -1) { 
   perror("bind"); 
   exit(1); 
   } 
if (listen(sockfd, BACKLOG) == -1) { 
   perror("listen"); 
   exit(1); 
   } 

while(1) { /* main accept() loop */ 
   sin_size = sizeof(struct sockaddr_in); 
   if ((new_fd = accept(sockfd, (struct sockaddr *)&amp;their_addr, \ 
   &amp;sin_size)) == -1) { 
   perror("accept"); 
   continue; 
   } 
   printf("server: got connection from %s\n", \ 
   inet_ntoa(their_addr.sin_addr)); 
   if (!fork()) { /* this is the child process */ 
   if (send(new_fd, "Hello, world!\n", 14, 0) == -1) 
   perror("send"); 
   close(new_fd); 
   exit(0); 
   } 
   close(new_fd); /* parent doesn't need this */ 
while(waitpid(-1,NULL,WNOHANG) >; 0); /* clean up child processes */ 
   } 
   } 
如果你很挑剔的话,一定不满意我所有的代码都在一个很大的main() 函数中。如果你不喜欢,可以划分得更细点。
你也可以用我们下一章中的程序得到服务器端发送的字符串。
--------------------------------------------------------------------------------
简单的客户程序 
  这个程序比服务器还简单。这个程序的所有工作是通过 3490 端口连接到命令行中指定的主机,然后得到服务器发送的字符串。 
客户代码: 
#include <stdio.h>;
  #include <stdlib.h>;
  #include <errno.h>;
  #include <string.h>;
  #include <sys/types.h>;
  #include <netinet/in.h>;
  #include <sys/socket.h>;
  #include <sys/wait.h>;
#define PORT 3490 /* 客户机连接远程主机的端口 */ 
#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */ 
int main(int argc, char *argv[]) 
   { 
   int sockfd, numbytes; 
   char buf[MAXDATASIZE]; 
   struct hostent *he; 
   struct sockaddr_in their_addr; /* connector's address information */ 
if (argc != 2) { 
   fprintf(stderr,"usage: client hostname\n"); 
   exit(1); 
   } 
if ((he=gethostbyname(argv[1])) == NULL) { /* get the host info */ 
   herror("gethostbyname"); 
   exit(1); 
   } 

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
   perror("socket"); 
   exit(1); 
   } 

their_addr.sin_family = AF_INET; /* host byte order */ 
  their_addr.sin_port = htons(PORT); /* short, network byte order */ 
  their_addr.sin_addr = *((struct in_addr *)he->;h_addr); 
  bzero(&amp;(their_addr.sin_zero),; /* zero the rest of the struct */ 
if (connect(sockfd, (struct sockaddr *)&amp;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",buf); 
close(sockfd); 
return 0; 
   } 
注意,如果你在运行服务器之前运行客户程序,connect() 将返回 "Connection refused" 信息,这非常有用。
--------------------------------------------------------------------------------
数据包 Sockets 
  我不想讲更多了,所以我给出代码 talker.c 和 listener.c。 
listener 在机器上等待在端口 4590 来的数据包。talker 发送数据包到 一定的机器,它包含用户在命令行输入的内容。 
这里就是 listener.c: 
#include <stdio.h>;
  #include <stdlib.h>;
  #include <errno.h>;
  #include <string.h>;
  #include <sys/types.h>;
  #include <netinet/in.h>;
  #include <sys/socket.h>;
  #include <sys/wait.h>;
#define MYPORT 4950 /* the port users will be sending to */ 
#define MAXBUFLEN 100 
main() 
   { 
   int sockfd; 
   struct sockaddr_in my_addr; /* my address information */ 
   struct sockaddr_in their_addr; /* connector's address information */ 
   int addr_len, numbytes; 
   char buf[MAXBUFLEN]; 
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 
   perror("socket"); 
   exit(1); 
   } 
my_addr.sin_family = AF_INET; /* host byte order */ 
   my_addr.sin_port = htons(MYPORT); /* short, network byte order */ 
   my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ 
   bzero(&amp;(my_addr.sin_zero),; /* zero the rest of the struct */ 
if (bind(sockfd, (struct sockaddr *)&amp;my_addr, sizeof(struct sockaddr)) 

   == -1) { 
   perror("bind"); 
   exit(1); 
   }
addr_len = sizeof(struct sockaddr); 
   if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \ 
   (struct sockaddr *)&amp;their_addr, &amp;addr_len)) == -1) { 
   perror("recvfrom"); 
   exit(1); 
   } 
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr)); 
   printf("packet is %d bytes long\n",numbytes); 
   buf[numbytes] = '\0'; 
   printf("packet contains \"%s\"\n",buf); 
close(sockfd); 
   } 
注意在我们的调用 socket(),我们最后使用了 SOCK_DGRAM。同时, 没有必要去使用 listen() 或者 accept()。我们在使用无连接的数据报套接 字! 
下面是 talker.c: 
#include <stdio.h>;
  #include <stdlib.h>;
  #include <errno.h>;
  #include <string.h>;
  #include <sys/types.h>;
  #include <netinet/in.h>;
  #include <sys/socket.h>;
  #include <sys/wait.h>;
#define MYPORT 4950 /* the port users will be sending to */ 
int main(int argc, char *argv[]) 
   { 
   int sockfd; 
   struct sockaddr_in their_addr; /* connector's address information */ 
   struct hostent *he; 
   int numbytes; 

if (argc != 3) { 
   fprintf(stderr,"usage: talker hostname message\n"); 
   exit(1); 
   } 

if ((he=gethostbyname(argv[1])) == NULL) { /* get the host info */ 
   herror("gethostbyname"); 
   exit(1); 
   } 

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 
   perror("socket"); 
   exit(1); 
   } 

their_addr.sin_family = AF_INET; /* host byte order */ 
   their_addr.sin_port = htons(MYPORT); /* short, network byte order 
*/ 
   their_addr.sin_addr = *((struct in_addr *)he->;h_addr); 
   bzero(&amp;(their_addr.sin_zero),; /* zero the rest of the struct */ 
if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, \ 
   (struct sockaddr *)&amp;their_addr, sizeof(struct sockaddr))) == -1) { 
   perror("sendto"); 
   exit(1); 
   } 
printf("sent %d bytes to 
%s\n",numbytes,inet_ntoa(their_addr.sin_addr)); 
close(sockfd); 
return 0; 
   } 
这就是所有的了。在一台机器上运行 listener,然后在另外一台机器上 运行 talker。观察它们的通讯!
除了一些我在上面提到的数据套接字连接的小细节外,对于数据套接 字,我还得说一些,当一个讲话者呼叫connect()函数时并指定接受者的地 址时,从这点可以看出,讲话者只能向connect()函数指定的地址发送和接 受信息。因此,你不需要使用sendto()和recvfrom(),你完全可以用send() 和recv()代替。
--------------------------------------------------------------------------------
阻塞 
  阻塞,你也许早就听说了。"阻塞"是 "sleep" 的科技行话。你可能注意 到前面运行的 listener 程序,它在那里不停地运行,等待数据包的到来。 实际在运行的是它调用 recvfrom(),然后没有数据,因此 recvfrom() 说" 阻塞 (block)",直到数据的到来。
很多函数都利用阻塞。accept() 阻塞,所有的 recv*() 函数阻塞。它 们之所以能这样做是因为它们被允许这样做。当你第一次调用 socket() 建 立套接字描述符的时候,内核就将它设置为阻塞。如果你不想套接字阻塞, 你就要调用函数 fcntl(): 
#include <unistd.h>;
  #include <fontl.h>;
   . 
   . 
   sockfd = socket(AF_INET, SOCK_STREAM, 0); 
   fcntl(sockfd, F_SETFL, O_NONBLOCK); 
   . 
   . 
  通过设置套接字为非阻塞,你能够有效地"询问"套接字以获得信息。如 果你尝试着从一个非阻塞的套接字读信息并且没有任何数据,它不允许阻 塞--它将返回 -1 并将 errno 设置为 EWOULDBLOCK。 
但是一般说来,这种询问不是个好主意。如果你让你的程序在忙等状 态查询套接字的数据,你将浪费大量的 CPU 时间。更好的解决之道是用 下一章讲的 select() 去查询是否有数据要读进来。
--------------------------------------------------------------------------------
select()--多路同步 I/O
  虽然这个函数有点奇怪,但是它很有用。假设这样的情况:你是个服 务器,你一边在不停地从连接上读数据,一边在侦听连接上的信息。 没问题,你可能会说,不就是一个 accept() 和两个 recv() 吗? 这么 容易吗,朋友? 如果你在调用 accept() 的时候阻塞呢? 你怎么能够同时接 受 recv() 数据? “用非阻塞的套接字啊!” 不行!你不想耗尽所有的 CPU 吧? 那么,该如何是好?
select() 让你可以同时监视多个套接字。如果你想知道的话,那么它就 会告诉你哪个套接字准备读,哪个又准备写,哪个套接字又发生了例外 (exception)。
闲话少说,下面是 select():
#include <sys/time.h>;
  #include <sys/types.h>;
  #include <unistd.h>;
int select(int numfds, fd_set *readfds, fd_set *writefds,fd_set 
*exceptfds, struct timeval *timeout);
这个函数监视一系列文件描述符,特别是 readfds、writefds 和 exceptfds。如果你想知道你是否能够从标准输入和套接字描述符 sockfd 读入数据,你只要将文件描述符 0 和 sockfd 加入到集合 readfds 中。参 数 numfds 应该等于最高的文件描述符的值加1。在这个例子中,你应该 设置该值为 sockfd+1。因为它一定大于标准输入的文件描述符 (0)。 当函数 select() 返回的时候,readfds 的值修改为反映你选择的哪个 文件描述符可以读。你可以用下面讲到的宏 FD_ISSET() 来测试。 在我们继续下去之前,让我来讲讲如何对这些集合进行操作。每个集 合类型都是 fd_set。下面有一些宏来对这个类型进行操作: 
FD_ZERO(fd_set *set) – 清除一个文件描述符集合
  FD_SET(int fd, fd_set *set) - 添加fd到集合 
  FD_CLR(int fd, fd_set *set) – 从集合中移去fd 
  FD_ISSET(int fd, fd_set *set) – 测试fd是否在集合中 
最后,是有点古怪的数据结构 struct timeval。有时你可不想永远等待 别人发送数据过来。也许什么事情都没有发生的时候你也想每隔96秒在终 端上打印字符串 "Still Going..."。这个数据结构允许你设定一个时间,如果 时间到了,而 select() 还没有找到一个准备好的文件描述符,它将返回让 你继续处理。 
数据结构 struct timeval 是这样的: 
struct timeval { 
   int tv_sec; /* seconds */ 
   int tv_usec; /* microseconds */ 
   }; 
只要将 tv_sec 设置为你要等待的秒数,将 tv_usec 设置为你要等待 的微秒数就可以了。是的,是微秒而不是毫秒。1,000微秒等于1毫秒,1,000 毫秒等于1秒。也就是说,1秒等于1,000,000微秒。为什么用符号 "usec" 呢? 字母 "u" 很象希腊字母 Mu,而 Mu 表示 "微" 的意思。当然,函数 返回的时候 timeout 可能是剩余的时间,之所以是可能,是因为它依赖于 你的 Unix 操作系统。 
哈!我们现在有一个微秒级的定时器!别计算了,标准的 Unix 系统 的时间片是100毫秒,所以无论你如何设置你的数据结构 struct timeval, 你都要等待那么长的时间。 
还有一些有趣的事情:如果你设置数据结构 struct timeval 中的数据为 0,select() 将立即超时,这样就可以有效地轮询集合中的所有的文件描述 符。如果你将参数 timeout 赋值为 NULL,那么将永远不会发生超时,即 一直等到第一个文件描述符就绪。最后,如果你不是很关心等待多长时间, 那么就把它赋为 NULL 吧。 
下面的代码演示了在标准输入上等待 2.5 秒: 
#include <sys/time.h>;
  #include <sys/types.h>;
  #include <unistd.h>;
#define STDIN 0 /* file descriptor for standard input */ 
main() 
   { 
  struct timeval tv; 
  fd_set readfds; 
tv.tv_sec = 2; 
  tv.tv_usec = 500000; 
FD_ZERO(&amp;readfds); 
  FD_SET(STDIN, &amp;readfds); 
/* don't care about writefds and exceptfds: */ 
  select(STDIN+1, &amp;readfds, NULL, NULL, &amp;tv); 
if (FD_ISSET(STDIN, &amp;readfds)) 
  printf("A key was pressed!\n"); 
  else 
  printf("Timed out.\n"); 
  } 
如果你是在一个 line buffered 终端上,那么你敲的键应该是回车 (RETURN),否则无论如何它都会超时。
现在,你可能回认为这就是在数据报套接字上等待数据的方式--你是对 的:它可能是。有些 Unix 系统可以按这种方式,而另外一些则不能。你 在尝试以前可能要先看看本系统的 man page 了。
最后一件关于 select() 的事情:如果你有一个正在侦听 (listen()) 的套 接字,你可以通过将该套接字的文件描述符加入到 readfds 集合中来看是 否有新的连接。
这就是我关于函数select() 要讲的所有的东西。
  参考书目: 
  Internetworking with TCP/IP, volumes I-III by Douglas E. Comer and 
David L. Stevens. Published by Prentice Hall. Second edition ISBNs: 
0-13-468505-9, 0-13-472242-6, 0-13-474222-2. There is a third edition of 
this set which covers IPv6 and IP over ATM. 
  Using C on the UNIX System by David A. Curry. Published by 
O'Reilly &amp; Associates, Inc. ISBN 0-937175-23-4. 
  TCP/IP Network Administration by Craig Hunt. Published by O'Reilly 
&amp; Associates, Inc. ISBN 0-937175-82-X. 
  TCP/IP Illustrated, volumes 1-3 by W. Richard Stevens and Gary R. 
Wright. Published by Addison Wesley. ISBNs: 0-201-63346-9, 
0-201-63354-X, 0-201-63495-3. 
Unix Network Programming by W. Richard Stevens. Published by 
Prentice Hall. ISBN 0-13-949876-1. 
  On the web: 
  BSD Sockets: A Quick And Dirty Primer 
  (http://www.cs.umn.edu/~bentlema/unix/--has other great Unix 
system programming info, too!) 
Client-Server Computing 
  (http://pandonia.canberra.edu.au/ClientServer/socket.html) 
Intro to TCP/IP (gopher) 

(gopher://gopher-chem.ucdavis.edu/11/Index/Internet_aw/Intro_the_Inter
net/intro.to.ip/) 
Internet Protocol Frequently Asked Questions (France) 
  (http://web.cnam.fr/Network/TCP-IP/) 
The Unix Socket FAQ 
  (http://www.ibrado.com/sock-faq/) 
RFCs--the real dirt: 
  RFC-768 -- The User Datagram Protocol (UDP) 
   (ftp://nic.ddn.mil/rfc/rfc768.txt) 
RFC-791 -- The Internet Protocol (IP) 
  (ftp://nic.ddn.mil/rfc/rfc791.txt) 
RFC-793 -- The Transmission Control Protocol (TCP) 
   (ftp://nic.ddn.mil/rfc/rfc793.txt) 
RFC-854 -- The Telnet Protocol 
   (ftp://nic.ddn.mil/rfc/rfc854.txt) 
RFC-951 -- The Bootstrap Protocol (BOOTP) 
 (ftp://nic.ddn.mil/rfc/rfc951.txt) 
RFC-1350 -- The Trivial File Transfer Protocol (TFTP) 
   (ftp://nic.ddn.mil/rfc/rfc1350.txt)
共3页: 上一页 [1] [2] [3] 下一页
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。