An article to learn to connect with Socket

An article to learn to connect with Socket


Once the connection is successfully established, the client and the server can exchange data through their respective sockets.  During the communication process, each socket has a unique identifier, which consists of four tuples (source IP address, source port number, destination, IP address destination port number).  This quadruple can uniquely identify a TCP connection.

In the TCP/IP protocol, a connection usually consists of two sockets, one is the client socket and the other is the server socket.

When the client wants to establish a connection with the server, it first creates a socket and specifies the IP address and port number of the server to be connected, and then sends a connection request to the server through the socket. The socket listening on  the specified port on the server side will receive the connection request, and create a new socket to establish a connection with the client's socket.  In this process, both the client and the server will have a socket for communication in the connection.

Once the connection is successfully established, the client and the server can exchange data through their respective sockets.  During the communication process, each socket has a unique identifier, which consists of four tuples (source IP address, source port number, destination, IP address destination port number). This quadruple can uniquely identify a TCP connection.   

Therefore, a TCP connection usually consists of two sockets, and each socket has its own unique identifier.  Both the socket on the client side and the socket on the server side play different roles, and they can communicate with each other after the connection is established .

In the Linux system, the following two parameters affect the number of TCP connections:

  1. net.core.somaxconn: This parameter is used to control the length of the waiting connection queue for each listening socket (such as server-side socket). In Linux 2.6 and later versions, the default value is 128, and the maximum value is the value specified by /proc/sys/net/core/somaxconn. However, it should be noted that even if a large waiting queue is set, it is not necessarily guaranteed that the system can accept and process so many connection requests.

In the TCP/IP protocol, when a client initiates a connection request to the server, the listening socket on the server will accept the request and establish a new connected socket for communicating with the client. However, if the server cannot process the connection request in time, the connection request will be placed in the socket waiting queue for processing by the server.

The Socket waiting queue is a first-in-first-out queue, which stores connection requests that have completed the three-way handshake, but the server has not yet accepted the connection. Each listening socket has its own waiting queue for storing connection requests from different clients. When a connection request arrives at the server, it will first be added to the waiting queue of the corresponding listening socket, waiting for the server to accept the connection.

The length of the waiting queue is controlled by kernel parameters, such as the net.core.somaxconn parameter in Linux is used to control the length of the waiting queue of each listening socket. When the waiting queue is full, new connection requests will be rejected, which means that the client cannot establish a connection.

  1. net.ipv4.tcp_max_syn_backlog: This parameter is used to control the length of the SYN queue in the TCP three-way handshake, that is, the semi-connection queue.  In Linux 2.2 and later versions, the default value is 128 and the maximum value is 65536.  If the SYN queue is full, new connection requests will be rejected.  However, it should be noted that the length of the SYN queue is only the length of the semi-connection queue, not equal to the number of TCP connections that the system can handle at the same time.

Therefore, the maximum number of TCP connections allowed depends on multiple factors such as system memory, current kernel parameter configuration, and network bandwidth.