当前位置:网站首页>What is the maximum number of concurrent TCP connections for a server? 65535?
What is the maximum number of concurrent TCP connections for a server? 65535?
2022-06-28 16:06:00 【51CTO】
First , What's described in the question 65535 The number of connections refers to the limit on the number of client connections .
stay tcp Application ,server Listen on a fixed port in advance ,client Initiate connection initiatively , After three handshakes tcp Connect . So for a single machine , Its maximum concurrency tcp What's the number of connections ?
How to identify a TCP Connect
Before determining the maximum number of connections , Let's see how the system identifies a tcp Connect . The system uses a 4 Four tuples to uniquely identify a TCP Connect :{localip, localport,remoteip,remoteport} = { Local ip, Local port, long-range ip, long-range port}
client Maximum tcp The number of connections
client Every time I initiate tcp Connection request , Unless the binding port , The system is usually asked to select an idle local port (local port), The port is exclusive , Not with anything else tcp Connect to share .tcp The data type of the port is unsigned short, Therefore, the maximum number of local ports is 65536, port 0 It has a special meaning , Out of commission , In this way, the most available ports are 65535, So in all as client In the case of end , One client Maximum tcp The number of connections is 65535, These connections can be connected to different serverip.
server Maximum tcp The number of connections
server Usually fixed on a local port to listen , wait for client Connection request for . Address reuse not considered (unix Of SO_REUSEADDR Options ) Under the circumstances , Even if server Multiple ends ip, The local listening port is also exclusive , therefore server End tcp Connect 4 Only tuples remoteip( That is to say clientip) and remote port( client port) Is variable , So the biggest tcp Connect as client ip Count × client port Count , Yes IPV4, Don't consider ip Address classification and other factors , Maximum tcp The number of connections is about 2 Of 32 Power (ip Count )×2 Of 16 Power (port Count ), That is to say server End stand-alone maximum tcp The number of connections is about 2 Of 48 Power .
Actually tcp The number of connections
The above is the theoretical maximum number of single machine connections , In the real world , Affected by machine resources 、 Operating system, etc , especially sever End , Its maximum concurrency tcp The number of connections is far from the theoretical limit . stay unix/linux The main factors limiting the number of connections are memory and the number of file descriptors allowed ( Every tcp All connections take up a certain amount of memory , Every socket It's just a file descriptor ), in addition 1024 The following ports are usually reserved ports .
therefore , Yes server End , By increasing memory 、 Modify the maximum number of file descriptors and other parameters , Single machine maximum concurrency TCP The number of connections exceeds 10 ten thousand , Even millions of them are OK .
This is obviously a misunderstanding of thinking ,65535 Is the total number of ports available , It doesn't mean that the server can only accept 65535 Two concurrent connections .
for instance :
We made a website , The binding is TCP Of 80 port , The result is that all users who visit the site are through the server 80 Port access , Not other ports . Visible ports are reusable .

Even if Linux The server is only 80 Port listening service , It's also allowed to have 10 ten thousand 、100 Ten thousand users connect to the server .Linux The system does not limit the number of connections, as to whether the server can withstand so many connections , Depending on the hardware configuration of the server 、 Software architecture and optimization .
01
We know that if two processes need to communicate, the most basic premise is : Can uniquely identify a process . We can use... In local process communication PID To uniquely identify a process , but PID It's only in the local area , Two processes in the network PID The odds of conflict are high .
At this time, we need to find another way ,IP The address can uniquely identify the host , and TCP Layer protocol and port number can uniquely identify a process of the host , This can be used IP Address + agreement + The port number uniquely identifies a process in the network .
After being able to uniquely identify the processes in the network , They can use socket We're communicating .socket( Socket ) It's an abstraction layer between the application layer and the transport layer , It is the TCP/IP Layer complex operation abstraction provides layer calls for several simple interfaces to realize the process communication in the network .

socket Derived from Unix, It's a kind of " open — read / Write — close " The realization of pattern , The server and the client each maintain one " file ", After the connection is established and opened , You can write content to your own file for the other party to read or read the other party's content , Close file at the end of communication .
02
The only way to be sure that a connection has 4 Something :
- Server's IP
- Server's Port
- Client's IP
- Client's Port
Server's IP and Port Can remain the same , As long as the client's IP and Port Different from each other can determine a connection number .

One socket Multiple connections can be made , One TCP The connection is marked as a quad (source_ip, source_port, destination_ip, destination_port), namely ( Source IP, Source port , Purpose IP, Destination port ) A combination of four elements . As long as one of the four elements is different , Then you can distinguish between different connections .
for instance :
Your host IP The address is 1.1.1.1, stay 8080 Port listening
When one comes from 2.2.2.2 There's a connection request , Port is 5555. The quadruple of this connection is (1.1.1.1, 8080, 2.2.2.2, 5555)
At this time 2.2.2.2 There's a second connection request , Port is 6666. The new connected quad is (1.1.1.1, 8080, 2.2.2.2, 6666)
that , Your host's 8080 Port establishes two connections ;
(2.2.2.2) Third connection request from , Port is 5555( or 6666). The third connection request cannot be established , Because there is no way to distinguish between the above two connections .
Empathy , You can use the same port number and IP Bind a TCP socket And a UDP socket
Because the port number is the same , But because the agreements are different , So the port number is completely independent .
TCP/UDP Five tuples are used to locate a connection :
source_ip, source_port, destination_ip, destination_port, protocol_type
namely ( Source IP, Source port , Purpose IP, Destination port , Agreement No )
in summary , The number of concurrent servers is not determined by TCP Of 65535 It's a port . The amount of concurrency that a server can sustain at the same time is determined by bandwidth 、 Hardware 、 Program design and other factors determine .
So I can understand Taobao 、 tencent 、 headlines 、 Baidu 、 Sina 、 Why can beep beep beep and so on withstand hundreds of millions of concurrent accesses per second , Because they use server clusters . Server clusters are distributed in large computer rooms all over the country , When the traffic is small, some servers will be shut down , When the traffic is heavy, new servers will be opened continuously .
65535 Where did you come from , Stem what of ?

To explain this well , We have to make it clear first 65535 The meaning of . stay Linux In the system , If two machines want to communicate , Then we need to establish a relationship between each other TCP Connect , In order to get to know each other ,Linux The system uses a quadruple to uniquely identify a TCP Connect :{local ip, local port, remote ip, remote port}, This is the machine IP、 Local port 、 long-range IP、 Remote port ,IP And port is equivalent to cell address and house number , Only by getting this information , The two sides of communication can know each other . stay Linux In the system , Represents the port number (port) Proportion of variables 16 position , This determines that the maximum number of ports is 2 Of 16 Power , namely 65536 individual , Another port 0 There is a special meaning not to be used , So each server has at most 65535 Ports available . therefore ,65535 representative Linux The system supports TCP Number of port numbers , stay TCP When you establish a connection, you use .
TCP How to establish a connection , What's the relationship with port number ?
Linux When the server interacts , There are generally two identities : Client or server . A typical Interaction scenario is :
(1) The server side actively creates listening socket, And bind the external service port port, And then start monitoring
(2) When the client wants to communicate with the server , Start connecting to the server port port
(3) The server accepts the request from the client , And then generate new socket
(4) The server and client are in the new socket We're communicating with each other
You can see , port port Mainly used in server and client “ Shake hands ” The process , Once we get to know each other , It will generate new socket communicate , Now port It's no longer necessary , You can give it to someone else socket Communication to use , So obviously TCP The number of connections can be greater than TCP The number of port numbers 65,535.
Consider two extreme scenarios , That is, a certain computer Linux The server is only the client or the server
(1)Linux The server serves only as the client
At this time, each one TCP request , The system will assign a free local port to you , And it's exclusive , Not by anyone else TCP The connection takes away , At most, you can set up 65535 A connection , Each connection interacts with a different server . This scenario , It's what the subject described , But because of the harsh conditions , It's a small probability event , So it's more likely in theory , It's almost impossible in a real world .
(2)Linux The server serves only as the server
In this case , The server will monitor the local port port, Waiting for the client to make a request to it . For simplicity of calculation , Let's assume that on the server side IP It's many to one with ports , such TCP In a quadruple there is remote ip and remote port Is variable , So maximum support for creating TCP The number is 2 Of 32 Power (IP The address is 32 Bit ) multiply 2 Of 16 Power (port yes 16 Bit ) be equal to 2 Of 48 Power .
In reality, a single station Linux Server support TCP Number of connections
Through the analysis above, we know that , In the real world , Due to the presence of ports port In the case of reuse , The server can support at the same time TCP The number of connections follows 65535 There is no one-to-one correspondence , in fact , Real impact TCP The number of connections , Is the memory of the server and the number of files that a single process is allowed to open at the same time , Because every time you create one TCP To create a connection socket Handle , Every socket Handles take up part of the system memory , When the system memory is used up , Allow the TCP The number of concurrent connections has reached the upper limit . In general , By increasing server memory 、 Modify the maximum number of file descriptors, etc , Single server support can be achieved 10 ten thousand + Of TCP Concurrent .

Of course , In real business scenarios , A single server is organized into a distributed cluster , Load balancing algorithm is used to dynamically schedule requests from different users to the most idle server , If the average server memory usage exceeds 80% The cordon of , Then we will adopt current limiting or cluster expansion to guarantee the service in time , Never run out of memory on the server , That's an accident .
All in all ,65535 It's just Linux Ports can be used in the system port The upper limit of quantity , port port Quantity and TCP The number of connections is not a one-to-one correspondence , Server support TCP The number of concurrent connections is mainly related to the memory of the server and the number of files that a single process is allowed to open at the same time , By means of port reuse and adjusting server parameters , Supported by a single server TCP The number of concurrent connections can be higher than 65535 Of .
边栏推荐
- 知道这几个命令让你掌握Shell自带工具
- How can the digital intelligent supply chain management platform of the smart Park optimize process management and drive the development of the park to increase speed and quality?
- Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
- ROS knowledge points - build an ROS development environment using vscode
- The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
- 简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
- Fleet | background Discovery issue 3: Status Management
- 5 minutes to make a bouncing ball game
- Do not use short circuit logic to write STL sorter multi condition comparison
- Soliciting articles and contributions - building a blog environment with a lightweight application server
猜你喜欢

Go zero micro Service Practice Series (VII. How to optimize such a high demand)

Qt5.5.1配置MSVC2010编绎器和windbg调试器

What useful supplier management systems are available

软件测试员的悲哀竟是...自己的技术能力不能满足大厂要求?

首次失败后,爱美客第二次冲刺港交所上市,财务负责人变动频繁
![[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels](/img/35/63c9793ec7bc1c90c759504e84dc96.png)
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels

关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力

大神详解开源 BUFF 增益攻略丨直播讲座

The world has embraced Web3.0 one after another, and many countries have clearly begun to seize the initiative

MIPS assembly language learning-01-sum of two numbers, environment configuration and how to run
随机推荐
Talking about open source - Linus and Jim talk about open source in China
软件测试员的悲哀竟是...自己的技术能力不能满足大厂要求?
Vc2010 compilation qt5.6.3 prompt cvtres: fatal error cvt1107:
FFmpeg之禁止输出banner log(三十)
零钱兑换(动态规划)
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
防火墙基础之流量管理与控制
关于针对tron API签名广播时使用curl的json解析问题解决方案及针对json.loads方法的问题记录
Qt 界面库
The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
IPDK — Overview
大神详解开源 BUFF 增益攻略丨直播讲座
首次失败后,爱美客第二次冲刺港交所上市,财务负责人变动频繁
早晨有些犹豫
Visual Studio 2010 编绎Qt5.6.3
看界面控件DevExpress WinForms如何创建一个虚拟键盘
NFT pledge LP liquidity mining system development details
Today's sleep quality record is 80 points
5 minutes to make a bouncing ball game
Application of mongodb in Tencent retail premium code