In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to test the maximum number of connections under Linux tcp method, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
There is a misunderstanding about the maximum number of concurrent connections to the TCP server, that is, "because the upper limit of the port number is 65535, the theoretical maximum number of concurrent connections that the TCP server can carry is 65535."
Let's start with the conclusion: for the TCP server process, the number of clients he can connect to at the same time is not limited by the available port number. The number of concurrent connections is limited by the number of files that can be opened by linux, which is configurable and can be very large, so it is actually limited by system performance.
Now do server development without high concurrency and have no face to go out, so in order to be criticized by others in the future, "improve concurrency every day, what is the highest concurrency you achieve", you can just go back, while New Year's Day has nothing to do at home, decide to write a demo and do it yourself.
The main purpose of this test is to find out which parameter configurations under Linux limit the maximum number of connections and what the upper limit is.
First, let's talk about demo's train of thought:
The server is implemented in epoll, which simply receives the connection, and then the client uses the goroutine of go, and each goroutine simply establishes the connection and then does nothing.
The above code:
Server:
/ * * grub +-o test_epoll. / test_epoll.c * / # include # include 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 = socket (AF_INET, SOCK_STREAM, 0); int iRet = SetReuseAddr (fd) If (iRet! = 0) {printf ("setsockopt for SO_REUSEADDR failed, error:%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 (bind (fd, (struct sockaddr*) & addr, sizeof (addr)) =-1) {printf ("bind failed, error:%s\ n", strerror (errno)); return errno;} if (listen (fd, 5) = =-1) {printf ("listen failed, error:%s\ n", strerror (errno)); return errno;} printf ("Listening on 8080.\ n"); int epfd = epoll_create (102400); struct epoll_event event; event.events = EPOLLIN Event.data.fd = fd; epoll_ctl (epfd, EPOLL_CTL_ADD, fd, & event); struct epoll_event revents [102400]; int iOnline = 0; while (1) {int num = epoll_wait (epfd, revents, 102400, 60 * 1000); printf ("epoll_wait return% d\ n", num); if (num > 0) {for (int I = 0; I)
< num; i++) { if (revents[i].data.fd == fd) { int client; struct sockaddr_in cli_addr; socklen_t cli_addr_len = sizeof(cli_addr); client = accept(fd, (struct sockaddr*)&cli_addr, &cli_addr_len); if (client == -1) { printf("accept failed, error:%s\n", strerror(errno)); if (errno == EMFILE) { printf("per-process limit reached\n"); exit(errno); } if (errno == ENFILE) { printf("system-wide limit reached\n"); exit(errno); } continue; } iOnline++; printf("Receive a new connection from %s:%d\n", inet_ntoa(cli_addr.sin_addr), cli_addr.sin_port); event.events = EPOLLIN; event.data.fd = client; epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event); } } } printf("Online number:%d\n", iOnline); } return 0;} client: package mainimport ( "net" "fmt" "time" "strconv" "runtime")func Connect(host string, port int) { _, err := net.Dial("tcp", host+":"+strconv.Itoa(port)) if err != nil { fmt.Printf("Dial to %s:%d failed\n", host, port) return } for { time.Sleep(30 * 1000 * time.Millisecond) }}func main() { count := 0 for { go Connect("192.168.63.128", 8080) count++; fmt.Printf("Gorutue num:%d\n", runtime.NumGoroutine()) time.Sleep(100 * time.Millisecond) }} 二、开始测试 第一次: 先说结果,连接数达到1031时accept失败了,当时还没有对errno做判断,所以只打印输出了accept失败。 然后首先想到的是ulimit -n的限制,查看了一下,默认值1024,然后就是修改这个值,在/etc/security/limits.conf中添加一下内容: 1 * soft nofile 1024002 * hard nofile 102400 然后关闭当前xshell连接,重新连接即生效,现在看ulimit -n就是102400了。 这两行的意思就是将每个进程能打开的文件描述符个数的soft、hard限制调整为102400, 注:ulimit -n 102400也可以生效,但是这个修改是临时的。 然后进行第二次测试。 第二次: 逗比了,其实连接数只有2000+,我之前还在奇怪为啥Windows的默认连接数能有这么高呢,原来有些连接已经断了,但是因为我没有做处理,所以以为还在呢,看来我得再安装一个虚拟机了[二哈] 待继续。。。 安装虚拟机去, 时间:2017-12-31 00:09:00 虚拟机安装好了,接着搞, 这次是真的超过10K了。The number of connections is still increasing. I don't know if it can finally reach 100000. I'm looking forward to ing.
Time: 2017-12-31 00:41:00, the final upper limit stuck in 28232 Golang kept reporting dial failure, because he forgot to print out the specific error message, so he had no way to know why dial failed, so he had to run dial again.
Time: 2017-12-31 01:01:00, add print dial failure error message, ran again, or at 28232 dial failure, error message:
There is no explanation of the error message in the standard library document of golang. From the error message, it is a failure to assign an address, so I wonder if the port address range is limited.
I looked at the port address range and confirmed that this is the limit. Because the port address is 16 bits, even if the port address range is changed to 1024, I can open a maximum of 64521 connections. But now I have only one virtual machine as a client, so it is impossible to achieve 100000 connections, but through this test, I have also figured out which parameters will limit the upper limit of connections. That's what I want.
Thank you for reading this article carefully. I hope the article "how to test the maximum number of tcp connections under Linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.