Official ChatGPT 4.0 and Claude Pro available for stable after-sales service
Author: Qianlang Langben Langliu Link: https://www.jianshu.com/p/f070212024a1
What is the maximum number of concurrent TCP connections?
First, the 65535 connections mentioned in the question refer to the limit on the number of client connections.
In TCP applications, the server listens on a fixed port, and the client actively initiates a connection. After a three-way handshake, a TCP connection is established. So, what is the maximum number of concurrent TCP connections for a single machine?
How a TCP connection is identified
Before determining the maximum number of connections, let’s see how the system identifies a TCP connection. The system uses a 4-tuple to uniquely identify a TCP connection: {localip, localport, remoteip, remoteport} = {local IP, local port, remote IP, remote port}
Maximum TCP connections for clients
When a client initiates a TCP connection request, unless a port is bound, the system typically selects an available local port (local port). This port is exclusive and cannot be shared with other TCP connections. The data type for TCP ports is unsigned short, so the maximum number of local ports is 65536. Port 0 has a special meaning and cannot be used, leaving a maximum of 65535 available ports. Therefore, in a scenario where all connections are client-side, the maximum number of TCP connections for a client is 65535, and these connections can connect to different server IPs.
Maximum TCP connections for servers
The server typically listens on a fixed local port, waiting for client connection requests. Without considering address reuse (the SO_REUSEADDR option in Unix), even if the server has multiple IPs, the local listening port is still exclusive. Therefore, in the server-side TCP connection 4-tuple, only the remote IP (client IP) and remote port (client port) are variable. Thus, the maximum TCP connections are the number of client IPs multiplied by the number of client ports. For IPv4, not considering IP address classification and other factors, the maximum number of TCP connections is approximately 2^32 (number of IPs) × 2^16 (number of ports), which means the maximum TCP connections for a single server is about 2^48.
Actual TCP connection count
The above gives the theoretical maximum number of connections for a single machine. In actual environments, due to machine resources, operating systems, etc., especially on the server side, the maximum number of concurrent TCP connections is far from reaching the theoretical limit. In Unix/Linux, the main factors limiting the number of connections are memory and the number of allowed file descriptors (each TCP connection occupies a certain amount of memory, and each socket is a file descriptor). Additionally, ports below 1024 are usually reserved.
Therefore, for the server side, by increasing memory and modifying the maximum number of file descriptors, it is possible for a single machine to exceed 100,000 concurrent TCP connections, even reaching millions.
This is clearly a misconception; 65535 refers to the total number of available ports and does not mean that the server can only accept 65535 concurrent connections at the same time.
For example:
If we set up a website that binds to TCP port 80, all users accessing this website do so through the server’s port 80, not other ports. This shows that ports can be reused.

Even if the Linux server only listens on port 80, it allows 100,000 or even 1,000,000 users to connect to the server. Whether the Linux system can handle so many connections depends on the server’s hardware configuration, software architecture, and optimization.
01We know that the most basic prerequisite for two processes to communicate is that they can uniquely identify a process. In local process communication, we can use PID to uniquely identify a process, but the PID is only unique locally, and the probability of PID conflicts in the network is high.
At this point, we need to find another way; the IP address can uniquely identify a host, while the TCP layer protocol and port number can uniquely identify a process on that host. Thus, we can use the IP address + protocol + port number to uniquely identify a process in the network.
Once we can uniquely identify processes in the network, they can communicate using sockets. A socket is an abstraction layer between the application layer and the transport layer, which abstracts the complex operations of the TCP/IP layer into several simple interfaces for the application layer to call for inter-process communication over the network.

Sockets originated from Unix and implement an “open-read/write-close” model, where the server and client each maintain a “file”. After establishing a connection, they can write content to their own file for the other party to read or read content from the other party, and close the file when communication ends. Search for the public account: Java Backend Programming on WeChat, reply: java to receive materials.
02Four elements uniquely determine a connection:
-
Server IP
-
Server Port
-
Client IP
-
Client Port
The server’s IP and port can remain unchanged as long as the client IP and port are different from each other, which can determine a connection count.

A socket can establish multiple connections, and a TCP connection is marked by a 4-tuple (source_ip, source_port, destination_ip, destination_port), i.e., a combination of (source IP, source port, destination IP, destination port). As long as one element in the combination is different, different connections can be distinguished.
For example:
Your host IP address is 1.1.1.1, listening on port 8080.
When a connection request comes from 2.2.2.2 on port 5555, the 4-tuple for this connection is (1.1.1.1, 8080, 2.2.2.2, 5555).
Then, 2.2.2.2 sends a second connection request on port 6666. The new connection’s 4-tuple is (1.1.1.1, 8080, 2.2.2.2, 6666).
At this point, your host’s port 8080 has established two connections;
The third connection request from (2.2.2.2) on port 5555 (or 6666) cannot be established because it cannot be distinguished from the above two connections.
Similarly, a TCP socket and a UDP socket can be bound to the same port number and IP address because although the port numbers are the same, the protocols are different, so the ports are completely independent.TCP/UDP generally use a 5-tuple to locate a connection:source_ip, source_port, destination_ip, destination_port, protocol_typei.e., (source IP, source port, destination IP, destination port, protocol number).
In summary, the number of concurrent connections on a server is not determined by the 65535 TCP ports. The number of concurrent connections that a server can handle simultaneously is determined by bandwidth, hardware, program design, and other factors.
Thus, it is understandable why companies like Taobao, Tencent, Toutiao, Baidu, Sina, and Bilibili can handle hundreds of millions of concurrent accesses per second; they use server clusters. Server clusters are distributed across large data centers nationwide, and when traffic is low, some servers are shut down, and when traffic is high, new servers are continuously activated.
Reprinted from https://blog.csdn.net/daocaokafei/article/details/115410761
Where does 65535 come from, and what is it for?

To explain this question well, we first need to clarify the meaning of 65535. In Linux systems, if two machines want to communicate, they need to establish a TCP connection. To allow both parties to recognize each other, the Linux system uses a 4-tuple to uniquely identify a TCP connection: {local IP, local port, remote IP, remote port}, which means local IP, local port, remote IP, and remote port. The IP and port are like the address of a neighborhood and the house number; only with this information can both parties communicate. In Linux systems, the variable representing the port number occupies 16 bits, which determines that there can be at most 2^16, or 65536 ports. Additionally, port 0 has a special meaning and cannot be used, so each server can have a maximum of 65535 available ports. Therefore, 65535 represents the number of TCP port numbers supported by the Linux system, which will be used when establishing TCP connections.
How does TCP establish a connection, and what is the relationship with port numbers?
When interacting, a Linux server generally has two identities: client or server. A typical interaction scenario is: (1) The server actively creates a listening socket, binds to the external service port, and starts listening. (2) When the client wants to communicate with the server, it starts connecting to the server’s port. (3) The server accepts the client’s request and then generates a new socket. (4) The server and client communicate over the new socket.
It can be seen that the port is mainly used in the “handshake” process of recognition between the server and client. Once they recognize each other, a new socket is generated for communication, and the port is no longer needed, allowing it to be used for other socket communications. Therefore, it is clear that the number of TCP connections can exceed the number of TCP ports, which is 65,535.
Consider two extreme scenarios, where a Linux server only acts as a client or server: (1) The Linux server only acts as a client.
In this case, each time a TCP request is initiated, the system will assign an available local port for you to use, and it is exclusive, meaning it will not be taken by other TCP connections. Thus, a maximum of 65535 connections can be established, each interacting with different servers. This scenario is what the question describes, but due to the overly stringent conditions, it is a low-probability event, so it is more of a theoretical possibility and rarely occurs in real environments.
(2) The Linux server only acts as a server.
In this scenario, the server will listen on a fixed local port, waiting for clients to initiate requests. For simplicity, let’s assume the server’s IP and port are many-to-one, so the TCP 4-tuple has variable remote IP and remote port, thus the maximum number of TCP connections supported is 2^32 (IP addresses are 32 bits) multiplied by 2^16 (ports are 16 bits), which equals 2^48.
Realistically, the number of TCP connections supported by a single Linux serverThrough the previous analysis, we know that in real scenarios, due to port reuse, the number of TCP connections that a server can support does not correspond one-to-one with 65535. In fact, the real factors affecting the number of TCP connections are the server’s memory and the number of allowed open files for a single process. Each time a TCP connection is created, a socket handle is created, and each socket handle occupies a portion of system memory. When the system memory is nearly exhausted, the allowed number of concurrent TCP connections also reaches its limit. Generally speaking, by increasing server memory and modifying the maximum number of file descriptors, a single server can support over 100,000 TCP concurrent connections.

Of course, in real commercial scenarios, a single server will be incorporated into a distributed cluster, dynamically scheduling different user requests to the least busy server through load balancing algorithms. If the average memory usage of the server exceeds 80% of the warning line, it will timely adopt throttling or expand the cluster to ensure service, and will never allow the server’s memory to be exhausted, as that would be a disaster.
In summary, 65535 is merely the upper limit of the number of available ports in the Linux system. The number of ports does not correspond one-to-one with the number of TCP connections. The number of concurrent TCP connections supported by a server is mainly related to the server’s memory and the number of files that a single process can open simultaneously. Through port reuse and adjusting server parameters, a single server can support more than 65535 TCP concurrent connections.
--End--
If you have read this far, it means you like the articles from this public account. Feel free to pin (star) this public account, Architect's Guide, so you can get notifications immediately!
In this public account, Architect's Guide, reply: architect to receive 2TB of learning materials!
Recommended reading
Unlimited use in China, official ChatGPT and Claude Pro with dual systems
Tsinghua University senior's self-study Linux notes, top-level quality!
Alibaba officially launched! SpringBoot + SpringCloud full-color guide