Linux网络编程 - TCP Socket 简单练习:select同时监测多个描述符

运行方式

服务器端,可以直接运行命令,也可以带参数运行,如果不带参数运行,则程序自主获取主机ip,然后默认设定port和lisnum的值。其中port默认为6666,lisnum默认为5.

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./socket_select_server IP地址(可选) 端口号(可选)监听队列大小(可选)    
客户端,必须指定服务器的IP地址和端口号,例如:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./socket_select_client 172.18.229.60 6666    

服务器代码

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /************************************************************************* 
  2.     > File Name: socket_select_server.c 
  3.     > Author: genglut 
  4.     > Mail: genglut@163.com 
  5.     > Created Time: 2014年12月22日 星期一 18时06分26秒 
  6.  ************************************************************************/  
  7.   
  8. /* 
  9. struct sockaddr_in  
  10. {   
  11.       short int sin_family;                // 地址协议   
  12.       in_port_t sin_port;                  // 端口号   
  13.       struct in_addr sin_addr;           // IP地址  
  14.       unsigned char sin_zero[8];        // 预留位  
  15. };   
  16.  
  17. struct in_addr  
  18. {   
  19.       _u32 s_addr;             // 32位地址   
  20. }; 
  21. */  
  22.   
  23.   
  24. #include <stdio.h>  
  25. #include <stdlib.h>  
  26. #include <errno.h>  
  27. #include <string.h>  
  28. #include <sys/types.h>  
  29. #include <netinet/in.h>  
  30. #include <sys/socket.h>  
  31. #include <sys/wait.h>  
  32. #include <unistd.h>  
  33. #include <arpa/inet.h>  
  34. #include <net/if.h>  
  35. #include <sys/ioctl.h>  
  36.   
  37. #define MAXBUF 1024  
  38.   
  39. void get_ip(char * str, char *ip);//获取本地IP地址  
  40.   
  41. int main(int argc, char *argv[])  
  42. {  
  43.       
  44.     //用于测试命令行输入的值的情况  
  45.     //printf("argv[0] = %s\n", argv[0]);      
  46.     //printf("argv[1] = %s\n", argv[1]);      
  47.     //printf("argv[2] = %s\n", argv[2]);      
  48.     //printf("argv[3] = %s\n", argv[3]);      
  49.   
  50.     int sockfd, newfd;  
  51.     socklen_t len;  
  52.     struct sockaddr_in server_addr, client_addr;//结构体sockaddr_in  
  53.     unsigned int server_port, lisnum;//用int也可以  
  54.     char buf[MAXBUF + 1];  
  55.       
  56.     //下面是select用到的变量的定义  
  57.     fd_set rfds;  
  58.     struct timeval tv;  
  59.     int retval;  
  60.     int maxfd = -1;  
  61.       
  62.     if(argv[1] && argv[2])//要根据argv[1]的情况来判断argv[2]的情况,否则会出错  
  63.         server_port = atoi(argv[2]);  
  64.     else  
  65.         server_port = 6666;  
  66.           
  67.     if(argv[1] && argv[2] && argv[3])//与上面道理相同  
  68.         lisnum = atoi(argv[3]);  
  69.     else  
  70.         lisnum = 5;   
  71.   
  72.     bzero(&server_addr, sizeof(server_addr));//也可以用memset  
  73.     server_addr.sin_family = AF_INET;  
  74.     server_addr.sin_port = htons(server_port);//转换为网络字节序  
  75.       
  76.     if(argv[1])  
  77.         //将ip地址转换为32位网络地址 inet_addr 也可以用inet_aton    
  78.         //inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr);  
  79.         server_addr.sin_addr.s_addr = inet_addr(argv[1]);  
  80.     else  
  81.     {  
  82.         char ip[128];  
  83.         get_ip("eno16777736", ip);  
  84.         server_addr.sin_addr.s_addr = inet_addr(ip);  
  85.     }  
  86.   
  87.     //建立sockfd  
  88.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  
  89.     {  
  90.         perror("socket");  
  91.         exit(EXIT_FAILURE);  
  92.     }  
  93.       
  94.     //输出ip和port信息,用于测试  
  95.     printf("server_ip = %s\nserver_port = %d\nlisnum = %d\n", inet_ntoa(server_addr.sin_addr), server_port, lisnum);  
  96.   
  97.     //绑定sockfd和服务器的IP地址server_addr  
  98.     if(bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)  
  99.     {  
  100.         perror("bind");  
  101.         exit(EXIT_FAILURE);  
  102.     }  
  103.       
  104.     //监听sockfd  
  105.     if(listen(sockfd, lisnum) == -1)  
  106.     {  
  107.         perror("listen");  
  108.         exit(EXIT_FAILURE);  
  109.     }  
  110.   
  111.       
  112.     while(1)  
  113.     {  
  114.         printf("\n---- wait for client connect ---\n");  
  115.   
  116.         //等待接收客户端的连接  
  117.         //连接成功后,客户端地址信息存储在client_addr中  
  118.         //新建立的socket描述符存储在newfd中  
  119.         len = sizeof(struct sockaddr);  
  120.         if((newfd = accept(sockfd, (struct sockaddr *)&client_addr, &len)) == -1)  
  121.         {  
  122.             perror("accept");  
  123.             exit(EXIT_FAILURE);  
  124.         }  
  125.         //打印客户端地址信息 inet_ntoa  ntohs  
  126.         printf("server: got connection from %s, port %d, socket %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), newfd);  
  127.   
  128.         while(1)  
  129.         {  
  130.             FD_ZERO(&rfds);//初始化rfds为空  
  131.             FD_SET(0, &rfds);//将标准输入的描述符0加入到集合rfds中  
  132.             FD_SET(newfd, &rfds);//将newfd加入到集合rfds中  
  133.             maxfd = newfd + 1;  
  134.             tv.tv_sec = 1;//阻塞等待时间为1s  
  135.             tv.tv_usec = 0;  
  136.               
  137.             retval = select(maxfd, &rfds, NULL, NULL, &tv);//多路复用,同时监测描述符0和newfd              
  138.             if(retval == -1)//select函数执行出错  
  139.             {  
  140.                 perror("select");  
  141.                 exit(EXIT_FAILURE);  
  142.             }  
  143.             else if(retval == 0)//select函数执行超时  
  144.                 continue;  
  145.             else//有描述符引起异常  
  146.             {  
  147.                 if(FD_ISSET(0, &rfds))//判断是不是标准输入0引起的异常  
  148.                 {  
  149.                     bzero(buf, sizeof(buf));//清空buf  
  150.                     fgets(buf, sizeof(buf)-1, stdin);//从终端接收输入  
  151.   
  152.                     if(!strncasecmp(buf, "quit", 4))//判断是否为退出  
  153.                     {  
  154.                         printf("i will close the connect!\n");  
  155.                         break;  
  156.                     }  
  157.                       
  158.                     len = send(newfd, buf, strlen(buf)-1, 0);//向客户端发送消息  
  159.                     if(len > 0)  
  160.                     {  
  161.                         printf ("send successful,%d byte send!\n",len);  
  162.                     }  
  163.                     else  
  164.                     {  
  165.                         printf("message '%s' send failure !\n", buf);  
  166.                         printf("errno code is %d, errno message is '%s'\n", errno, strerror(errno));  
  167.                         break;  
  168.                     }                     
  169.                 }  
  170.                   
  171.                 if(FD_ISSET(newfd, &rfds))//判断是不是newfd引起的异常  
  172.                 {  
  173.                     bzero(buf, sizeof(buf));  
  174.                     len = recv(newfd, buf, sizeof(buf)-1, 0);//从客户端接收消息  
  175.                     if(len > 0 )  
  176.                         printf("message recv successful : '%s', %d Byte recv\n", buf, len);  
  177.                     else if(len < 0)  
  178.                     {  
  179.                         printf("recv failure !\nerrno code is %d, errno message is '%s'\n", errno, strerror(errno));  
  180.                         break;  
  181.                     }  
  182.                     else//如果客户端已关闭  
  183.                     {  
  184.                         printf("the other one close quit\n");  
  185.                         break;  
  186.                     }                     
  187.                 }  
  188.             }         
  189.         }  
  190.         close(newfd);  
  191.         printf("need other connection ? (no -> quit) : ");  
  192.         fflush(stdout);  
  193.         bzero(buf, sizeof(buf));//清空buf  
  194.         fgets(buf, sizeof(buf)-1, stdin);//从终端接收输入  
  195.         if(!strncasecmp(buf, "no", 2))//判断是否继续等待连接  
  196.         {  
  197.             printf("quit!\n");  
  198.             break;  
  199.         }     
  200.     }     
  201.   
  202.     close(sockfd);  
  203.   
  204.     return 0;  
  205. }  
  206.   
  207.   
  208. void get_ip(char * str, char *ip)  
  209. {  
  210.     int inet_sock;  
  211.     struct ifreq ifr;  
  212.     inet_sock = socket(AF_INET, SOCK_DGRAM, 0);  
  213.     strcpy(ifr.ifr_name, str);//#include <net/if.h>  
  214.     if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0)//#include <sys/ioctl.h>  
  215.     {  
  216.         perror("ioctl");  
  217.         exit(EXIT_FAILURE);  
  218.     }  
  219.     sprintf(ip,"%s", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));  
  220.     close(inet_sock);     
  221. }  

客户端代码

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /************************************************************************* 
  2.     > File Name: socket_select_client.c 
  3.     > Author: genglut 
  4.     > Mail: genglut@163.com 
  5.     > Created Time: 2014年12月22日 星期一 18时06分06秒 
  6.  ************************************************************************/  
  7.   
  8. #include <stdio.h>  
  9. #include <string.h>  
  10. #include <errno.h>  
  11. #include <sys/socket.h>  
  12. #include <resolv.h>  
  13. #include <stdlib.h>  
  14. #include <netinet/in.h>  
  15. #include <arpa/inet.h>  
  16. #include <unistd.h>  
  17.   
  18. #define MAXBUF 1024  
  19.   
  20. int main(int argc, char *argv[])  
  21. {  
  22.     int sockfd;  
  23.     socklen_t len;  
  24.     struct sockaddr_in server_addr;  
  25.     char buf[MAXBUF + 1];  
  26.   
  27.     //下面是select用到的变量的定义  
  28.     fd_set rfds;  
  29.     struct timeval tv;  
  30.     int retval;  
  31.     int maxfd = -1;  
  32.       
  33.     if(argc != 3)  
  34.     {  
  35.         printf("error failure, it must be:\n\t\t%s IP port \n", argv[0]);  
  36.         exit(EXIT_FAILURE);  
  37.     }  
  38.   
  39.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  
  40.     {  
  41.         perror("socket");  
  42.         exit(EXIT_FAILURE);  
  43.     }  
  44.   
  45.     bzero(&server_addr, sizeof(server_addr));  
  46.     server_addr.sin_family = AF_INET;  
  47.     server_addr.sin_port = htons(atoi(argv[2]));  
  48.     server_addr.sin_addr.s_addr = inet_addr(argv[1]);  
  49.   
  50.     if(connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)  
  51.     {  
  52.         perror("connect");  
  53.         exit(EXIT_FAILURE);  
  54.     }  
  55.   
  56.     printf("already connected to server %s\n", argv[1]);  
  57.       
  58.       
  59.     while(1)  
  60.     {  
  61.         FD_ZERO(&rfds);//初始化rfds为空  
  62.         FD_SET(0, &rfds);//将标准输入的描述符0加入到集合rfds中  
  63.         FD_SET(sockfd, &rfds);//将newfd加入到集合rfds中  
  64.         maxfd = sockfd + 1;  
  65.         tv.tv_sec = 1;//阻塞等待时间为1s  
  66.         tv.tv_usec = 0;  
  67.           
  68.         retval = select(maxfd, &rfds, NULL, NULL, &tv);//多路复用,同时监测描述符0和newfd  
  69.           
  70.         if(retval == -1)//select函数执行出错  
  71.         {  
  72.             perror("select");  
  73.             exit(EXIT_FAILURE);  
  74.         }  
  75.         else if(retval == 0)//select函数执行超时  
  76.             continue;  
  77.         else//有描述符引起异常  
  78.         {  
  79.             if(FD_ISSET(0, &rfds))//判断是不是标准输入0引起的异常  
  80.             {  
  81.                 bzero(buf, sizeof(buf));//清空buf  
  82.                 fgets(buf, sizeof(buf)-1, stdin);//从终端接收输入  
  83.   
  84.                 if(!strncasecmp(buf, "quit", 4))//判断是否为退出  
  85.                 {  
  86.                     printf("i will quit!\n");  
  87.                     break;  
  88.                 }  
  89.                   
  90.                 len = send(sockfd, buf, strlen(buf)-1, 0);//向客户端发送消息  
  91.                 if(len > 0)  
  92.                 {  
  93.                     printf ("send successful,%d byte send!\n",len);  
  94.                 }  
  95.                 else  
  96.                 {  
  97.                     printf("message '%s' send failure !\n", buf);  
  98.                     printf("errno code is %d, errno message is '%s'\n", errno, strerror(errno));  
  99.                     break;  
  100.                 }                     
  101.             }  
  102.               
  103.             if(FD_ISSET(sockfd, &rfds))//判断是不是newfd引起的异常  
  104.             {  
  105.                 bzero(buf, sizeof(buf));  
  106.                 len = recv(sockfd, buf, sizeof(buf)-1, 0);//从客户端接收消息  
  107.                 if(len > 0 )  
  108.                     printf("message recv successful : '%s', %d Byte recv\n", buf, len);  
  109.                 else if(len < 0)  
  110.                 {  
  111.                     printf("recv failure !\nerrno code is %d, errno message is '%s'\n", errno, strerror(errno));  
  112.                     break;  
  113.                 }  
  114.                 else//如果客户端已关闭  
  115.                 {  
  116.                     printf("the other one close, quit\n");  
  117.                     break;  
  118.                 }                     
  119.             }  
  120.         }     
  121.     }  
  122.       
  123.     close(sockfd);  
  124.     printf("i quited!\n");  
  125.     return 0;  
  126. }  

原文链接

http://blog.csdn.net/geng823/article/details/42099569

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值