介绍
这篇文章主要介绍了怎么测试Linux下tcp最大连接数的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。
关于tcp服务器最大并发连接数有一种误解就是“因为端口号上限为65535,所以tcp服务器理论上的可承载的最大并发连接数也是65535”。
先说结论:对于tcp服务端进程来说,他可以同时连接的客户端数量并不受限于可用端口号。并发连接数受限于Linux可打开文件数,这个数是可以配置的,可以非常大,所以实际上受限于系统性能。
现在做服务器开发不加上高并发根本没脸出门,所以为了以后吹水被别人怼“天天提高并发,你自己实现的最高并发是多少”的时候能义正言辞的怼回去,趁着元旦在家没事决定自己写个演示搞一搞。
这个测试主要是想搞明白Linux下哪些参数配置限制了连接数的最大值,上限是多少。
<强>一,先说下演示的思路:强>
服务端用epoll实现,就是简简单单的接收连接,然后客户端用去的goroutine,每个goroutine就是简单的建立连接,然后什么也不做。
上代码:
服务器:
/* ,* g++, -o test_epoll 。/test_epoll.c ,*/# include & lt; unistd.h> # include & lt; sys/types.h> # include & lt; sys/socket.h> # include & lt; sys/epoll.h> # include & lt; netinet/in.h> # include & lt; arpa/inet.h> # include & lt; stdio.h> # include & lt; stdlib.h> # include & lt; string.h> # include & lt; errno.h> int SetReuseAddr (int fd) { ,int optval =, 1; ,socklen_t optlen =, sizeof (optval); ,return setsockopt (fd, SOL_SOCKET,, SO_REUSEADDR,,, optval,, optlen); } int main () { ,int fd =,插座(AF_INET, SOCK_STREAM,, 0); ,int iRet =, SetReuseAddr (fd); ,if (iRet !=, 0) ,{ ,printf (“setsockopt  for SO_REUSEADDR 失败了,,错误:% s \ n",, strerror (iRet)); ,return iRet; ,} ,struct sockaddr_in  addr; ,memset(及addr, 0, sizeof (addr));=,,addr.sin_family  AF_INET;=,,addr.sin_port  htons (8080);=,,addr.sin_addr.s_addr  INADDR_ANY; ,if (绑定(fd, (struct sockaddr *), addr,, sizeof (addr)),==, 1) ,{ ,printf (“bind 失败了,,错误:% s \ n",, strerror (errno)); ,return errno; ,} ,if (听(fd,,,==, 1) ,{ ,printf (“listen 失败了,,错误:% s \ n",, strerror (errno)); ,return errno; ,} ,printf (“Listening 提醒8080…\ n"); ,int epfd =, epoll_create (102400); ,struct epoll_event 事件;=,,event.events  EPOLLIN;=,,event.data.fd  fd; ,epoll_ctl (epfd, EPOLL_CTL_ADD,, fd,,,事件); ,struct epoll_event 防止[102400]; ,int iOnline =, 0; ,while (1) ,{ ,int num =, epoll_wait (epfd,防止,,102400年,60岁,*,1000); ,printf (“epoll_wait  return % d \ n",, num); ,if (num 祝辞,0) ,{ for 才能;(int 小姐:=,0;,小姐:& lt;, num;,我+ +) {才能 if 才能;(防止[我].data.fd ==, fd) {才能 ,,int 客户端; ,,struct sockaddr_in  cli_addr; ,,socklen_t cli_addr_len =, sizeof (cli_addr); ,,client =,接受(fd,, (struct sockaddr *), cli_addr,,, cli_addr_len); ,,if (client ==, 1) ,,{ ,,printf (“accept 失败,错误:% s \ n",, strerror (errno)); ,,if (errno ==, EMFILE) ,,{ ,,,printf (“per-process  limit 达到\ n"); ,,,退出(errno); ,,} ,,if (errno ==, ENFILE) ,,{ ,,,printf (“system-wide  limit 达到\ n"); ,,,退出(errno); ,,} ,才能继续; ,,} ,,iOnline + +; ,,printf (“Receive  a new connection 得到% s: % d \ n",, inet_ntoa (cli_addr.sin_addr), cli_addr.sin_port); ,,event.events =, EPOLLIN; ,,event.data.fd =,客户端; ,,epoll_ctl (epfd, EPOLL_CTL_ADD,, fd,,,事件); ,,} ,,} ,} ,printf (“Online 号码:% d \ n",, iOnline); ,} ,return 0; }
客户:
package 主要 import ( ,“net" ,“fmt" ,“time" ,“strconv" ,“runtime" ) 字符串,func 连接(host port int), { ,_,err :=, net.Dial (“tcp",,主机+“:“+ strconv.Itoa(港口)) ,if err  !=, nil { ,fmt.Printf (“Dial 用% s: % d 失败\ n",,主机,端口) ,返回 ,} ,for { ,time . sleep (30, *, 1000, *, time.Millisecond) ,} } func main (), { ,count :=0 null null null null null null null null怎么测试Linux下tcp最大连接数的方法