Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the maximum number of connections between TCP server and client?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the maximum number of connections between the TCP server and the client. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

As long as the resources (memory hard disk cpu) are sufficient, it is theoretically possible to receive unlimited links. The so-called 65535 limit is for the client. Every time the client links a service, it must open a tcp port corresponding to it. In this way, after linking to 65535 services, the local port is full. Only one service port is used. A tcp link is determined by four elements, server IP:port+client IP:port. (local ip, local port,remote ip,remote port)

For the server, each tcp connection occupies a file descriptor, and once this file descriptor is used up, an error is returned.

We know that the port number below 1024 on the operating system is reserved by the system and is used by users from 1024 to 65535. Since each TCP connection has a port number, we can have at most more than 60000 concurrent connections. But this is not the case.

We also know that for TCP connections are made up of: the original IP, the original port, the destination IP, and the destination port. But only remote ip (that is, client ip) and remote port (client port) are variable in the 4 tuples of listening server TCP connection. Because it is fixed on the local port to listen, the maximum TCP connection is: number of client ip × number of client port

For the client, the server listens to a fixed port in the process of network communication, and the client initiates a connection request after three handshakes to establish a TCP connection with the server. Each time a client initiates a TCP connection, the system randomly selects a free port, which is exclusive and cannot be shared with other TCP connections, so in theory, a machine can initiate as many TCP connections as it has free ports.

According to the TCP/IP protocol, port port is stored using a 16-bit unsigned integer unsigned short, so there are a total of 2 ^ 16 = 65536 local ports, that is, 0-65535, of which 0 is a reserved port, 0 has a special meaning that cannot be used, and ports below 1024 can only be used by super administrator users (such as root), so even with root permission, the maximum number of ports that a machine can use is only 65535. But a machine can only use a maximum of 28232 ports.

Let's explain in detail the two common sense of file handle restrictions and port restrictions.

Common sense 1: file handle restrictions

Friends who write web server programs under linux must know that every tcp connection takes up a file descriptor. Once this file descriptor is used up, the error returned to us by the new connection is "Socket/File:Can't open so many files".

At this point, you need to understand the operating system's limit on the maximum number of files that can be opened.

Process restriction

Execute ulimit-n output 1024, indicating that a process can only open up to 1024 files, so you can use this default configuration up to thousands of TCP connections.

Temporary modification: ulimit-n 1000000, but this temporary modification is only valid for the current environment of the currently logged-in user, and will expire after the system restart or the user exits.

Invalid changes after restart (but I tested it under CentOS 6.5.No failure was found after restart): edit / etc/security/limits.conf file, the modified content is

Soft nofile 1000000

Hard nofile 1000000

Permanent modification: edit / etc/rc.local and add the following after that

Ulimit-SHn 1000000

Global restriction

Execute cat / proc/sys/fs/file-nr output 9344 592026, respectively: 1. Number of file handles that have been allocated, 2. Number of file handles that have been allocated but not used, 3. The maximum number of file handles. But in kernel 2.6, the value of the second item is always 0, which is not an error, it actually means that the assigned file descriptors have been used without any waste.

We can make this value larger and modify the / etc/sysctl.conf file with root permissions:

Fs.file-max = 1000000

Net.ipv4.ip_conntrack_max = 1000000

Net.ipv4.netfilter.ip_conntrack_max = 1000000

Common sense 2: limit the range of port numbers?

The port number below 1024 on the operating system is reserved by the system and is used by the user from 1024 to 65535. Since each TCP connection has a port number, we can have at most more than 60000 concurrent connections. I think there is such a wrong idea that friends are not in the minority, right? (that's what I used to think.)

Let's analyze it.

How to identify a TCP connection: the system uses a 4-quad to uniquely identify a TCP connection: {local ip, local port,remote ip,remote port}. All right, let's take a look at the conceptual aspects of accept in Chapter 4 of "UNIX Network programming: volume 1." the second parameter, cliaddr, represents the ip address and port number of the client. As a server, we only use this port of bind, which means that the port number 65535 is not a concurrency limit.

Maximum number of server tcp connections: server usually listens on a local port and waits for client connection requests. Without considering address reuse (SO_REUSEADDR option of unix), even if there are multiple ip on the server side, the local listening port is exclusive, so only remote ip (that is, client ip) and remote port (client port) are variable in the server tcp connection 4 tuple, so the maximum tcp connection is the number of client ip × client port. For IPV4, the ip address classification and other factors are not considered. The maximum number of tcp connections is about 2 to the power of 32 (ip) × 2 to the power of 16 (port), that is, the maximum number of tcp connections on the server side is about 2 to the 48th power.

The above is the maximum number of connections between TCP server and client. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report