公告:CSDN 搜索第二版正式上线!
CSDN大会PPT合集下载,纯干货!
2000元大奖征异构开发博文
2012CSDN博客之星评选正式上线 【免费】解读海外市场营销奥秘 CSDN博客频道推出TAG功能
2012CSDN博客之星评选正式上线 【免费】解读海外市场营销奥秘 CSDN博客频道推出TAG功能
1.select 模型是一个比较传统的异步IO模型,我们知道的著名的apache就是基于select模型,而我之前工作过的搜狐畅游的天龙八部,还有几款游戏都是基于select模型。
对于select模型,大多都是说他的缺点,实际上我的观点有点不一样,select模型的跨平台性是比较好的,开发也比较简单,只有当个进程连接数的限制,以及其性能随着连接数增长下降的问题,实际上都得根据项目的实际情况而定的。在内部分布式通讯中,几乎所有的连接都是活跃的情况下,select模型并不比epoll的性能差,只是在一些应用中大部分连接都不活跃的情况,epoll的使用效果要高,可以使单台服务器的承载量大大增加,实际上游戏服务器中玩家绝大部发是活跃的,因此实际性能也还不错。
下面是我自己写的一个简单的select模型的demo,其中要注意两个地方,估计写过select的同学很多都遇到类似的问题,一个是超时的变量实际上每次select后需要重新赋值,否则可能出现一只超时的情况, 二就是fd_set 每次select后,不活跃的会清零,需要一个备份的。再个就是保存客户端socket需要一个数组,队列,或者其他数据结构都可以,要注意增加,清除的问题。
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<errno.h>
- #include<netdb.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<netdb.h>
- #include<sys/time.h>
- #include<string.h>
- #include<sys/select.h>
- #include<pthread.h>
- using namespace std;
- int max_fd(int a[], int n)
- {
- int max = 0;
- for(int i = 0; i < n; i++)
- {
- if(max < a[i])
- {
- max = a[i];
- }
- }
- return max;
- }
- int main(int argc, char*argv[])
- {
- int port = 0;
- int N = 0;
- if (argc != 3)
- {
- cout<<"command error"<<endl;
- exit(-1);
- }
- port = atoi(argv[1]);
- N = atoi(argv[2]);
- if(N > FD_SETSIZE)
- {
- N = FD_SETSIZE;
- }
- int server_sock = 0;
- struct sockaddr_in server_addr;
- memset(&server_addr, 0, sizeof(server_addr));
- if((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- cout<<"create socket error"<<endl;
- exit(-1);
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.s_addr =htonl(INADDR_ANY);
- server_addr.sin_port = htons(port);
- if(bind(server_sock, (struct sockaddr*)&server_addr,sizeof(sockaddr))
- == -1)
- {
- cout<<"bind error"<<endl;
- exit(-1);
- }
- if(listen(server_sock, 5) == -1)
- {
- cout<<"listent error"<<endl;
- exit(-1);
- }
- fd_set fd[2];
- FD_ZERO(&fd[0]);
- FD_SET(server_sock, &fd[0]);
- int *sock = new int[N];
- memset(sock, 0, sizeof(int)*N);
- sock[0] = server_sock;
- int count = 0;
- while(1)
- {
- struct timeval tv = {5, 0};
- FD_ZERO(&fd[1]);
- fd[1] = fd[0];
- int ret = select(max_fd(sock, N)+1, &fd[1], NULL, NULL, &tv);
- if(ret < 0)
- {
- cout<<"select error"<<endl;
- }
- else if(ret == 0)
- {
- cout<<"time out"<<endl;
- }
- else
- {
- if(FD_ISSET(sock[0], &fd[1]) && count < N-1)
- {
- struct sockaddr_in client_addr;
- memset(&client_addr, 0, sizeof(client_addr));
- unsigned int len = sizeof(client_addr);
- int new_sock=accept(sock[0],(struct sockaddr*)&client_addr, &len);
- if(new_sock == -1)
- {
- cout<<"accept error"<<endl;
- }
- else
- {
- for(int i = 1; i < N; i++)
- {
- if(sock[i] == 0)
- {
- sock[i] = new_sock;
- FD_SET(new_sock, &fd[0]);
- count++;
- break;
- }
- }
- }
- }
- char recvbuf[1024] = {0};
- char sendbuf[1024] = {0};
- for(int i = 1; i < N; i++)
- {
- if(FD_ISSET(sock[i], &fd[1]))
- {
- if(recv(sock[i], recvbuf, sizeof(recvbuf), 0) <= 0)
- {
- cout<<"recv error"<<endl;
- FD_CLR(sock[i], &fd[0]);
- close(sock[i]);
- sock[i] = 0;
- count--;
- continue;
- }
- strcpy(sendbuf, recvbuf);
- if(send(sock[i], sendbuf, sizeof(sendbuf), 0) <= 0)
- {
- cout<<"send error"<<endl;
- FD_CLR(sock[i], &fd[0]);
- close(sock[i]);
- sock[i] = 0;
- count--;
- continue;
- }
- }
- }//end for
- }
- }//end while
- return 0;
- }