http://blog.csdn.net/chenxun_2010/article/details/50489577
一、用select实现的并发服务器,能达到的并发数,受两方面限制
1、一个进程能打开的最大文件描述符限制。这可以通过调整内核参数。可以通过ulimit -n来调整或者使用setrlimit函数设置, 但一个系统所能打开的最大数也是有限的,跟内存大小有关,可以通过cat /proc/sys/fs/file-max 查看
2、select中的fd_set集合容量的限制(FD_SETSIZE,一般为1024) ,这需要重新编译内核。
可以写个测试程序,只建立连接,看看最多能够建立多少个连接,客户端程序如下:
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
-
- #define ERR_EXIT(m) \
- do \
- { \
- perror(m); \
- exit(EXIT_FAILURE); \
- } while(0)
-
-
- int main(void)
- {
- int count = 0;
- while(1)
- {
- int sock;
- if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- {
- sleep(4);
- ERR_EXIT("socket");
- }
-
- struct sockaddr_in servaddr;
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(5188);
- servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
- if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
- ERR_EXIT("connect");
-
- struct sockaddr_in localaddr;
- socklen_t addrlen = sizeof(localaddr);
- if (getsockname(sock, (struct sockaddr *)&localaddr, &addrlen) < 0)
- ERR_EXIT("getsockname");
-
- printf("ip=%s port=%d\n", inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port));
- printf("count = %d\n", ++count);
-
- }
-
- return 0;
- }
服务器的代码serv.c
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<errno.h>
- #include<arpa/inet.h>
- #include<netinet/in.h>
- #include<string.h>
- #include<signal.h>
- #include<sys/wait.h>
-
-
-
- #define ERR_EXIT(m) \
- do { \
- perror(m); \
- exit(EXIT_FAILURE); \
- } while (0)
-
-
- int main(void)
- {
-
- signal(SIGPIPE, SIG_IGN);
- int listenfd;
- if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
-
- ERR_EXIT("socket error");
-
- struct sockaddr_in servaddr;
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(5188);
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-
-
- int on = 1;
- if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
- ERR_EXIT("setsockopt error");
-
- if (bind(listenfd, (struct sockaddr*)&servaddr,sizeof(servaddr)) < 0)
- ERR_EXIT("bind error");
-
- if (listen(listenfd, SOMAXCONN) < 0)
- ERR_EXIT("listen error");
-
- struct sockaddr_in peeraddr;
- socklen_t peerlen = sizeof(peeraddr);
-
- int conn;
- int i;
- int client[FD_SETSIZE];
- int maxi = 0;
- for (i = 0; i < FD_SETSIZE; i++)
- client[i] = -1;
-
- int nready;
- int maxfd = listenfd;
- fd_set rset;
- fd_set allset;
- FD_ZERO(&rset);
- FD_ZERO(&allset);
- FD_SET(listenfd, &allset);
-
- int count = 0;
- while (1) {
- rset = allset;
- nready = select(maxfd + 1, &rset, NULL, NULL, NULL);
- if (nready == -1) {
- if (errno == EINTR)
- continue;
- ERR_EXIT("select error");
- }
-
- if (nready == 0)
- continue;
-
- if (FD_ISSET(listenfd, &rset)) {
-
- conn = accept(listenfd, (struct sockaddr*)&peeraddr, &peerlen);
- if (conn == -1)
- ERR_EXIT("accept error");
- printf("count = %d\n", ++count);
- for (i = 0; i < FD_SETSIZE; i++) {
- if (client[i] < 0) {
- client[i] = conn;
- if (i > maxi)
- maxi = i;
- break;
- }
- }
-
- if (i == FD_SETSIZE) {
- fprintf(stderr, "too many clients\n");
- exit(EXIT_FAILURE);
- }
-
- printf("recv connect ip=%s port=%d\n", inet_ntoa(peeraddr.sin_addr),
- ntohs(peeraddr.sin_port));
-
- FD_SET(conn, &allset);
- if (conn > maxfd)
- maxfd = conn;
-
- if (--nready <= 0)
- continue;
- }
-
- for (i = 0; i <= maxi; i++) {
- conn = client[i];
- if (conn == -1)
- continue;
-
- if (FD_ISSET(conn, &rset)) {
-
- char recvbuf[1024] = {0};
- int ret = read(conn, recvbuf, 1024);
- if (ret == -1)
- ERR_EXIT("read error");
- else if (ret == 0) {
- printf("client close \n");
- FD_CLR(conn, &allset);
- client[i] = -1;
- close(conn);
- }
-
- fputs(recvbuf, stdout);
- write(conn, recvbuf, strlen(recvbuf));
-
- if (--nready <= 0)
- break;
- }
- }
-
-
- }
-
- return 0;
- }
-
-
-
-
-
-
huangcheng@ubuntu:~$ ./serv
count = 1
recv connect ip=127.0.0.1 port=48370
count = 2
recv connect ip=127.0.0.1 port=48371
count = 3
recv connect ip=127.0.0.1 port=48372
count = 4
recv connect ip=127.0.0.1 port=48373
....................................
recv connect ip=127.0.0.1 port=49389
count = 1020
recv connect ip=127.0.0.1 port=49390
accept error: Too many open files
[cpp] view plaincopyprint?
huangcheng@ubuntu:~$ ./cli
ip=127.0.0.1 port=46327
count = 1
ip=127.0.0.1 port=46328
count = 2
ip=127.0.0.1 port=46329
count = 3
ip=127.0.0.1 port=46330
count = 4
ip=127.0.0.1 port=46331
count = 5
ip=127.0.0.1 port=46332
count = 6
ip=127.0.0.1 port=46333
.......................
ip=127.0.0.1 port=47345
count = 1020
ip=127.0.0.1 port=47346
count = 1021
socket: Too many open files
输出太多条目,上面只截取最后几条,从中可以看出对于客户端,最多只能开启1021个连接套接字,因为总共是1024个,还得除去0、1、2。而服务器端只能accept 返回1020个已连接套接字,因为除了0、1、2之外还有一个监听套接字,客户端某一个套接字(不一定是最后一个)虽然已经建立了连接,在已完成连接队列中,但accept 返回时达到最大描述符限制,返回错误,打印提示信息。
也许有人会注意到上面有一行 sleep(4);当客户端调用socket准备创建第1022个套接字时,如上所示也会提示错误,此时socket函数返回-1出错,如果没有睡眠4s后再退出进程会有什么问题呢?如果直接退出进程,会将客户端所打开的所有套接字关闭掉,即向服务器端发送了很多FIN段,而此时也许服务器端还一直在accept ,即还在从已连接队列中返回已连接套接字,此时服务器端除了关心监听套接字的可读事件,也开始关心前面已建立连接的套接字的可读事件,read 返回0,所以会有很多 client close 字段 参杂在条目的输出中,还有个问题就是,因为read 返回0,服务器端会将自身的已连接套接字关闭掉,那么也许刚才说的客户端某一个连接会被accept 返回,即测试不出服务器端真正的并发容量。
- huangcheng@ubuntu:~$ ./serv
- count = 1
- recv connect ip=127.0.0.1 port=50413
- count = 2
- ....................................
- client close
- client close
- client close
- client close
- ...................................
- recv connect ip=127.0.0.1 port=51433
- client close
- count = 1021
- recv connect ip=127.0.0.1 port=51364
- client close
- client close
可以看到输出参杂着client close,且这次的count 达到了1021,原因就是服务器端前面已经有些套接字关闭了,所以accept 创建套接字不会出错,服务器进程也不会因为出错而退出,可以看到最后接收到的一个连接端口是51364,即不一定是客户端的最后一个连接。
二、poll 函数应用举例
- #include <poll.h>
- int poll(struct pollfd *fds, nfds_t nfds, int timeout);
参数1:结构体数组指针
- struct pollfd {
- int fd;
- short events;
- short revents;
- };
结构体中的fd 即套接字描述符,events 即感兴趣的事件,如下图所示,revents 即返回的事件。
![](https://img-blog.csdn.net/20130611144105312)
参数2:结构体数组的成员个数,即文件描述符个数。
参数3:即超时时间,若为-1,表示永不超时。
poll 跟 select 还是很相似的,比较重要的区别在于poll 所能并发的个数跟FD_SETSIZE无关,只跟一个进程所能打开的文件描述符个数有关,可以在select 程序的基础上修改成poll 程序,在运行服务器端程序之前,使用ulimit -n 2048 将限制改成2048个,注意在运行客户端进程的终端也需更改,因为客户端也会有所限制,这只是临时性的更改,因为子进程会继承这个环境参数,而我们是在bash命令行启动程序的,故在进程运行期间,文件描述符的限制为2048个。
使用poll 函数的服务器端程序如下:
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<errno.h>
- #include<arpa/inet.h>
- #include<netinet/in.h>
- #include<string.h>
- #include<signal.h>
- #include<sys/wait.h>
- #include<poll.h>
-
- #define ERR_EXIT(m) \
- do { \
- perror(m); \
- exit(EXIT_FAILURE); \
- } while (0)
-
-
- int main(void)
- {
- int count = 0;
- signal(SIGPIPE, SIG_IGN);
- int listenfd;
- if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
-
- ERR_EXIT("socket error");
-
- struct sockaddr_in servaddr;
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(5188);
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-
-
- int on = 1;
- if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
- ERR_EXIT("setsockopt error");
-
- if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
- ERR_EXIT("bind error");
-
- if (listen(listenfd, SOMAXCONN) < 0)
- ERR_EXIT("listen error");
-
- struct sockaddr_in peeraddr;
- socklen_t peerlen = sizeof(peeraddr);
-
- int conn;
- int i;
-
- struct pollfd client[2048];
- int maxi = 0;
-
- for (i = 0; i < 2048; i++)
- client[i].fd = -1;
-
- int nready;
- client[0].fd = listenfd;
- client[0].events = POLLIN;
-
- while (1)
- {
-
- nready = poll(client, maxi + 1, -1);
- if (nready == -1)
- {
- if (errno == EINTR)
- continue;
- ERR_EXIT("poll error");
- }
-
- if (nready == 0)
- continue;
-
- if (client[0].revents & POLLIN)
- {
-
- conn = accept(listenfd, (struct sockaddr *)&peeraddr, &peerlen);
- if (conn == -1)
- ERR_EXIT("accept error");
-
- for (i = 1; i < 2048; i++)
- {
- if (client[i].fd < 0)
- {
- client[i].fd = conn;
- if (i > maxi)
- maxi = i;
- break;
- }
- }
-
- if (i == 2048)
- {
- fprintf(stderr, "too many clients\n");
- exit(EXIT_FAILURE);
- }
-
- printf("count = %d\n", ++count);
- printf("recv connect ip=%s port=%d\n", inet_ntoa(peeraddr.sin_addr),
- ntohs(peeraddr.sin_port));
-
- client[i].events = POLLIN;
-
- if (--nready <= 0)
- continue;
- }
-
- for (i = 1; i <= maxi; i++)
- {
- conn = client[i].fd;
- if (conn == -1)
- continue;
- if (client[i].revents & POLLIN)
- {
-
- char recvbuf[1024] = {0};
- int ret = read(conn, recvbuf, 1024);
- if (ret == -1)
- ERR_EXIT("readline error");
- else if (ret == 0)
- {
- printf("client close \n");
- client[i].fd = -1;
- close(conn);
- }
-
- fputs(recvbuf, stdout);
- write(conn, recvbuf, strlen(recvbuf));
-
- if (--nready <= 0)
- break;
- }
- }
-
-
- }
-
- return 0;
- }
-
-
参照前面对
select 函数
的解释不难理解上面的程序,就不再赘述了。来看一下输出:
- root@ubuntu:/home/huangcheng# ulimit -n 2048
- root@ubuntu:/home/huangcheng# su - huangcheng
- huangcheng@ubuntu:~$ ulimit -n
- 2048
- huangcheng@ubuntu:~$ ./serv
- ...........................
- count = 2042
- recv connect ip=127.0.0.1 port=54499
- count = 2043
- recv connect ip=127.0.0.1 port=54500
- count = 2044
- recv connect ip=127.0.0.1 port=54501
- accept error: Too many open files
- root@ubuntu:/home/huangcheng# ulimit -n 2048
- root@ubuntu:/home/huangcheng# su - huangcheng
- huangcheng@ubuntu:~$ ulimit -n
- 2048
- huangcheng@ubuntu:~$./cli
- ..........................
- ip=127.0.0.1 port=54499
- count = 2043
- ip=127.0.0.1 port=54500
- count = 2044
- ip=127.0.0.1 port=54501
- count = 2045
- socket: Too many open files
可以看到现在最大的连接数已经是2045个了,虽然服务器端有某个连接没有accept 返回。即poll 比 select 能够承受更多的并发连接,只受一个进程所能打开的最大文件描述符个数限制。可以通过ulimit -n 修改,但一个系统所能打开的文件描述符个数也是有限的,这跟系统的内存大小有关系,所以说也不是可以无限地并
发,可以查看一下本机的容量:
- huangcheng@ubuntu:~$ cat /proc/sys/fs/file-max
- 101598
本机是虚拟机,内存2G,能够打开的文件描述符个数大约在10w个左右。