一般情況下,Linux 下 TCP 連線的數量上限受 FD_SETSIZE 影響,系統預設為 1024。若要突破這一限制,可以從以下幾個方向著手。
修改 /etc/security/limits.conf,加入以下內容:
* soft nofile 20000 * hard nofile 20000
接著重新開機,伺服器便可建立多達 20000 個連線。此時的連線方式是直接使用 connect、accept,但要注意,這裡不能搭配 select。
上述方法若不搭配 select 是可以的,但如果你使用 select,仍然只能開啟 1024 個連線,因為 select 的數量由 FD_SETSIZE 決定。此時可以改用 poll 來替代 select——poll 的陣列大小可依我們自己的需要來定義,這樣就解決了這個問題。
Linux 是透過檔案方式來管理系統的,因此系統能承載多少 TCP 連線,與系統檔案開啟數目的能力密切相關。此外,/proc/sys/file-max 中定義了系統最多能開啟的檔案數目。
#include <iostream>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <sys/poll.h>
#include <limits.h> /* for OPEN_MAX */
#define LPORT 3333
#define LISTENQ 1024
#define OPEN_MAX 50000
#define MAX_LINE 100
void setnonblocking(int sock)
{
int opts;
opts = fcntl(sock, F_GETFL);
if (opts < 0) { perror("fcntl(sock,GETFL)"); exit(1); }
opts = opts | O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0) { perror("fcntl(sock,SETFL,opts)"); exit(1); }
}
int main(int argc, char **argv)
{
int listenfd;
struct sockaddr_in cliaddr; // 客戶端的 sock 描述
struct sockaddr_in servaddr; // 服務器的 sock 描述
struct pollfd *array_conn;
array_conn = new pollfd[OPEN_MAX];
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket create error!"); exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(LPORT);
/* 設定 socket 可重複使用 */
int opt = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
close(listenfd); perror("setsockopt error!"); exit(1);
}
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
perror("bind error!"); exit(1);
}
listen(listenfd, LISTENQ);
array_conn[0].fd = listenfd;
array_conn[0].events = POLLRDNORM;
int i;
for (i = 1; i < OPEN_MAX; i++) array_conn[i].fd = -1; /* -1 表示可用條目 */
int maxi = 0;
int connfd, nready, clilen, x = 0;
size_t n;
for ( ; ; ) {
nready = poll(array_conn, maxi + 1, 0);
if (array_conn[0].revents & POLLRDNORM) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, (socklen_t*)&clilen);
printf(" ||%d: Connection from %s:%d\n", x++,
inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
for (i = 1; i < OPEN_MAX; i++)
if (array_conn[i].fd < 0) { array_conn[i].fd = connfd; break; }
if (i == OPEN_MAX) { printf("too many clients"); break; }
array_conn[i].events = POLLRDNORM;
if (i > maxi) maxi = i;
if (--nready <= 0) continue; /* 沒有更多可讀描述符 */
}
/* 檢查所有客戶端是否有資料 */
char line[100];
int sockfd;
for (i = 1; i <= maxi; i++) {
if ((sockfd = array_conn[i].fd) < 0) continue;
if (array_conn[i].revents & (POLLRDNORM | POLLERR)) {
if ((n = read(sockfd, line, MAX_LINE)) < 0) {
if (errno == ECONNRESET) { close(sockfd); array_conn[i].fd = -1; }
else { printf("readline error\n"); }
} else if (n == 0) {
close(sockfd); array_conn[i].fd = -1; /* 客戶端關閉連線 */
} else {
char sb[10];
printf("Recv:%s from socket %d", line, sockfd);
sprintf(sb, "Pong!");
write(sockfd, sb, 10);
}
if (--nready <= 0) break; /* 沒有更多可讀描述符 */
}
}
}
}
對應的客戶端測試程式會持續建立連線、送出 "Ping*" 並接收服務器的 "Pong!" 回覆,可用來驗證伺服器在高連線數下的承載能力。