Testing TCP Maximum connection limit under Linux

Source: Internet
Author: User

Do server development now do not add high concurrency is not ashamed to go out, so in order to blow water by others godless "every day to improve concurrency, you achieve the highest concurrency is how much" when the words can be justified godless back, while New Year's Day at home nothing decided to write a demo to engage in.

This test is mainly to understand which parameter configurations under Linux limit the maximum number of connections, and what the upper limit is.

First, the idea of the demo:

Service End With Epoll implementation, is a simple receive connection, then the client with Go goroutine, each goroutine is simple to establish a connection, and then do nothing.

On the code:

Server

1 /*2 * g++-o test_epoll./TEST_EPOLL.C3  */4#include <unistd.h>5#include <sys/types.h>6#include <sys/socket.h>7#include <sys/epoll.h>8#include <netinet/inch.h>9#include <arpa/inet.h>Ten  One#include <stdio.h> A#include <stdlib.h> -#include <string.h> -#include <errno.h> the  - intSETREUSEADDR (intFD) - { -     intOptval =1; +socklen_t Optlen =sizeof(optval); -     returnSetSockOpt (FD, Sol_socket, SO_REUSEADDR, &optval, Optlen); + } A  at intMain () - { -     intFD = socket (af_inet, Sock_stream,0); -     intIRet =setreuseaddr (FD); -     if(IRet! =0) -     { inprintf"setsockopt for So_reuseaddr failed, error:%s\n", Strerror (IRet)); -         returnIRet; to     } +  -     structsockaddr_in addr; thememset (&AMP;ADDR,0,sizeof(addr)); *addr.sin_family =af_inet; $Addr.sin_port = htons (8080);Panax NotoginsengADDR.SIN_ADDR.S_ADDR =Inaddr_any; -     if(Bind (FD, (structsockaddr*) &addr,sizeof(addr)) == -1) the     { +printf"bind failed, error:%s\n", Strerror (errno)); A         returnerrno; the     } +  -     if(FD, listen5) == -1) $     { $printf"Listen failed, error:%s\n", Strerror (errno)); -         returnerrno; -     } theprintf"Listening on 8080...\n"); - Wuyi     intEPFD = Epoll_create (102400); the     structEpoll_eventEvent; -     Event. Events =Epollin; Wu     Event. DATA.FD =FD; -Epoll_ctl (EPFD, Epoll_ctl_add, FD, &Event); About  $     structEpoll_event revents[102400]; -     intIonline =0; -      while(1) -     { A         intnum = epoll_wait (EPFD, Revents,102400, -* +); +printf"epoll_wait return%d\n", num); the         if(Num >0) -         { $              for(inti =0; i < num; i++) the             { the                 if(REVENTS[I].DATA.FD = =FD) the                 { the                     intclient; -                     structsockaddr_in cli_addr; insocklen_t Cli_addr_len =sizeof(CLI_ADDR); theClient = Accept (FD, (structsockaddr*) &cli_addr, &Cli_addr_len); the                     if(Client = =-1) About                     { theprintf"accept failed, error:%s\n", Strerror (errno)); the                         if(errno = =emfile) the                         { +printf"per-process Limit reached\n"); - exit (errno); the                         }Bayi                         if(errno = =enfile) the                         { theprintf"system-wide Limit reached\n"); - exit (errno); -                         } the                         Continue; the                     } the  theionline++; -printf"Receive A new connection from%s:%d\n", Inet_ntoa (cli_addr.sin_addr), cli_addr.sin_port); the                     Event. Events =Epollin; the                     Event. DATA.FD =client; theEpoll_ctl (EPFD, Epoll_ctl_add, FD, &Event);94                 } the             } the         } theprintf"Online number:%d\n", ionline);98     } About  -     return 0;101}

Client

1 Package Main2 3 Import (4     "Net"5     "FMT"6     " Time"7     "StrConv"8     "Runtime"9 )Ten  OneFunc Connect (Hoststring, Portint) { A_, Err: = Net. Dial ("TCP", host+":"+StrConv. Itoa (port)) -     ifErr! =Nil { -Fmt. Printf ("Dial to%s:%d failed\n", host, port) the         return -     } -  -      for { +Time. Sleep ( -* +*Time.millisecond) -     } + } A  at Func Main () { -Count: =0 -      for { -Go Connect ("192.168.63.128",8080) -count++; -Fmt. Printf ("Gorutue num:%d\n", runtime. Numgoroutine ()) inTime. Sleep ( -*Time.millisecond) -     } to}

Note: Blog Park Code Editor Incredibly still does not support go, now go with a lot of people, hope fast support AH.

Second, start testing for the first time:

First of all, the connection number reached 1031 when the accept failed, then did not Judge errno, so only print out the accept failed.

Then the first thought is the ulimit-n limit, look at the default value of 1024, and then modify this value, add the content in/etc/security/limits.conf:

1 *    soft    1024002 *    hard    102400

Then close the current Xshell connection, reconnect is effective, now see Ulimit-n is 102400.

These two lines mean that the number of file descriptors that can be opened by each process is soft, hard limit is adjusted to 102400,

Note: Ulimit-n 102400 can also take effect, but this modification is temporary.

Then perform a second Test.

Second time:

Tease, in fact, the number of connections is only 2000 +, I was wondering why the default number of Windows connections can be so high, some connections have been broken, but because I did not deal with it, so I think it is still there, it seems I have to install another virtual machine

Wait to continue ...

Install the virtual machine to

Testing TCP Maximum connection limit under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.