一、用select实现的并发服务器,能达到的并发数,受两方面限制
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; //被动套接字(文件描述符),即只可以accept, 监听套接字
if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
// listenfd = socket(AF_INET, SOCK_STREAM, 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);
/* servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
/* inet_aton("127.0.0.1", &servaddr.sin_addr); */
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) //listen应在socket和bind之后,而在accept之前
ERR_EXIT("listen error");
struct sockaddr_in peeraddr; //传出参数
socklen_t peerlen = sizeof(peeraddr); //传入传出参数,必须有初始值
int conn; // 已连接套接字(变为主动套接字,即可以主动connect)
int i;
int client[FD_SETSIZE];
int maxi = 0; // client数组中最大不空闲位置的下标
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); //accept不再阻塞
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;
}
/* select所能承受的最大并发数受
* 1.一个进程所能打开的最大文件描述符数,可以通过ulimit -n来调整
* 但一个系统所能打开的最大数也是有限的,跟内存有关,可以通过cat /proc/sys/fs/file-max 查看
* 2.FD_SETSIZE(fd_set)的限制,这个需要重新编译内核
*/
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出错ÿ