select

来源:互联网 发布:数据分析师华为 编辑:程序博客网 时间:2024/05/24 06:34


注:此为复制别人的文章,怕找不到,记录在此.


1.select 模型是一个比较传统的异步IO模型,我们知道的著名的apache就是基于select模型,而我之前工作过的搜狐畅游的天龙八部,还有几款游戏都是基于select模型。

对于select模型,大多都是说他的缺点,实际上我的观点有点不一样,select模型的跨平台性是比较好的,开发也比较简单,只有当个进程连接数的限制,以及其性能随着连接数增长下降的问题,实际上都得根据项目的实际情况而定的。在内部分布式通讯中,几乎所有的连接都是活跃的情况下,select模型并不比epoll的性能差,只是在一些应用中大部分连接都不活跃的情况,epoll的使用效果要高,可以使单台服务器的承载量大大增加,实际上游戏服务器中玩家绝大部发是活跃的,因此实际性能也还不错。

 

下面是我自己写的一个简单的select模型的demo,其中要注意两个地方,估计写过select的同学很多都遇到类似的问题,一个是超时的变量实际上每次select后需要重新赋值,否则可能出现一只超时的情况, 二就是fd_set 每次select后,不活跃的会清零,需要一个备份的。再个就是保存客户端socket需要一个数组,队列,或者其他数据结构都可以,要注意增加,清除的问题。

[cpp] view plain copy print?
  1. #include<iostream>  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  
  4. #include<unistd.h>  
  5. #include<errno.h>  
  6. #include<netdb.h>  
  7. #include<sys/types.h>  
  8. #include<sys/socket.h>  
  9. #include<netinet/in.h>  
  10. #include<arpa/inet.h>  
  11. #include<netdb.h>  
  12. #include<sys/time.h>  
  13. #include<string.h>  
  14. #include<sys/select.h>  
  15. #include<pthread.h>  
  16. using namespace std;  
  17.   
  18. int max_fd(int a[], int n)  
  19. {  
  20.     int max = 0;  
  21.     for(int i = 0; i < n; i++)  
  22.     {  
  23.         if(max < a[i])  
  24.         {  
  25.             max = a[i];  
  26.         }  
  27.     }  
  28.       
  29.     return  max;  
  30. }  
  31.   
  32. int main(int argc, char*argv[])  
  33. {     
  34.     int port = 0;  
  35.     int N = 0;  
  36.     if (argc != 3)  
  37.     {  
  38.         cout<<"command error"<<endl;  
  39.         exit(-1);  
  40.     }  
  41.   
  42.     port = atoi(argv[1]);  
  43.     N = atoi(argv[2]);  
  44.     if(N > FD_SETSIZE)  
  45.     {  
  46.       N =   FD_SETSIZE;  
  47.     }  
  48.     int server_sock = 0;  
  49.     struct sockaddr_in server_addr;  
  50.     memset(&server_addr, 0, sizeof(server_addr));  
  51.   
  52.     if((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)  
  53.     {  
  54.         cout<<"create socket error"<<endl;  
  55.         exit(-1);  
  56.     }  
  57.   
  58.     server_addr.sin_family = AF_INET;  
  59.     server_addr.sin_addr.s_addr =htonl(INADDR_ANY);  
  60.     server_addr.sin_port = htons(port);  
  61.       
  62.     if(bind(server_sock, (struct sockaddr*)&server_addr,sizeof(sockaddr))  
  63.        == -1)  
  64.     {  
  65.         cout<<"bind error"<<endl;  
  66.         exit(-1);  
  67.     }     
  68.       
  69.     if(listen(server_sock, 5) == -1)  
  70.     {     
  71.         cout<<"listent error"<<endl;  
  72.         exit(-1);  
  73.     }  
  74.       
  75.     fd_set fd[2];  
  76.     FD_ZERO(&fd[0]);  
  77.     FD_SET(server_sock, &fd[0]);  
  78.     int *sock = new int[N];  
  79.     memset(sock, 0, sizeof(int)*N);  
  80.     sock[0] = server_sock;  
  81.     int count = 0;  
  82.       
  83.     while(1)  
  84.     {  
  85.        struct timeval tv = {5, 0};  
  86.        FD_ZERO(&fd[1]);  
  87.        fd[1] = fd[0];  
  88.        int ret = select(max_fd(sock, N)+1, &fd[1], NULL, NULL, &tv);  
  89.        if(ret < 0)  
  90.        {  
  91.         cout<<"select error"<<endl;  
  92.        }  
  93.        else if(ret == 0)  
  94.        {  
  95.         cout<<"time out"<<endl;  
  96.        }  
  97.        else  
  98.        {  
  99.         if(FD_ISSET(sock[0], &fd[1]) && count < N-1)  
  100.         {  
  101.            struct sockaddr_in client_addr;  
  102.            memset(&client_addr, 0, sizeof(client_addr));  
  103.            unsigned int len = sizeof(client_addr);  
  104.            int new_sock=accept(sock[0],(struct sockaddr*)&client_addr, &len);  
  105.            if(new_sock == -1)  
  106.            {  
  107.             cout<<"accept error"<<endl;  
  108.            }  
  109.            else  
  110.                {  
  111.             for(int i = 1; i < N; i++)  
  112.             {  
  113.                  if(sock[i] == 0)  
  114.                  {  
  115.                 sock[i] = new_sock;                               
  116.                 FD_SET(new_sock, &fd[0]);  
  117.                 count++;  
  118.                 break;  
  119.                  }  
  120.             }  
  121.            }  
  122.   
  123.         }  
  124.           
  125.         char recvbuf[1024] = {0};  
  126.         char sendbuf[1024] = {0};  
  127.         for(int i = 1; i < N; i++)  
  128.         {  
  129.             if(FD_ISSET(sock[i], &fd[1]))  
  130.             {  
  131.             if(recv(sock[i], recvbuf, sizeof(recvbuf), 0) <= 0)  
  132.             {     
  133.                 cout<<"recv error"<<endl;  
  134.                 FD_CLR(sock[i], &fd[0]);  
  135.                 close(sock[i]);  
  136.                 sock[i] = 0;  
  137.                 count--;  
  138.                 continue;  
  139.             }  
  140.   
  141.             strcpy(sendbuf, recvbuf);  
  142.   
  143.             if(send(sock[i], sendbuf, sizeof(sendbuf), 0) <= 0)  
  144.             {     
  145.                 cout<<"send error"<<endl;  
  146.                 FD_CLR(sock[i], &fd[0]);  
  147.                 close(sock[i]);  
  148.                 sock[i] = 0;  
  149.                 count--;  
  150.                 continue;  
  151.             }  
  152.             }  
  153.       
  154.         }//end for   
  155.   
  156.        }  
  157.           
  158.     }//end while  
  159.     return 0;  
  160.   
  161. }  
原创粉丝点击