This article is reprinted from WeChat public account: Linwan Village Chinchilla

TCP is a connection-oriented protocol. Before either party sends data in any direction, a connection must be established between the two parties. The state transition diagram for connection creation and termination is as follows:

The process is as follows:
-
The client sends a SYN packet indicating that it intends to connect to the server’s port, initializing the sequence number (ISN) to m.
-
The server responds with its ISN (value n) as an acknowledgment. At the same time, it sets the acknowledgment number to client ISN + 1 (m + 1) as confirmation of the client’s SYN.
-
The client sends an ACK packet, ack = n + 1, as confirmation of the server’s SYN.
1. Why Three-Way Handshake Instead of Two?
<span>The network is unreliable, and packets may be lost.</span>
Suppose there is no third acknowledgment; the client sends a SYN to the server to request a connection. Due to delays, the server does not receive this packet in time. The client then resends a SYN packet. Recall the sequence numbers mentioned when introducing the TCP header; these two packets have the same sequence number. Suppose the server receives the second SYN packet and establishes communication, and after a while, the communication ends, and the connection is closed. At this point, the initially sent SYN packet just arrives at the server, and the server sends an ACK confirmation again. Since the connection was established with just two handshakes, the server will establish a new connection, but the client believes it did not request a connection and will not send data to the server, leading to a wasted empty connection on the server.
<span>TCP</span><span> is duplex and requires bidirectional confirmation.</span>
With only two handshakes, the client knows the server received it, but the server does not know the client received it, analogous to making a phone call. This is the Byzantine Generals problem in communication systems.
2. Maximum Segment Size
The Maximum Segment Size (MSS) indicates the maximum block of data that TCP can send to the other end. When a connection is established, both parties must announce their MSS. This is determined by the optional field in the TCP header during the three-way handshake. The default length for Ethernet is 1460.
3.

Establishing a connection requires a three-way handshake, while terminating a connection requires four handshakes. This is due to TCP’s half-close mechanism. A TCP connection is full-duplex (i.e., data can be transmitted simultaneously in both directions), so each direction must be closed separately.
-
The active party wants to close the connection and sends a FIN packet to the passive party, sequence number m.
-
The passive party receives the FIN packet sent by the active party, acknowledges that the other party wants to close the connection, and sends an ACK confirmation packet, sequence number m + 1. The active party closes the connection.
-
Wait for a moment (in half-closed state), during which the passive party sends the last data, and the active party receives the last data.
-
The passive party confirms the closure of the connection and sends a FIN packet, sequence number n.
-
The active party waits for a moment (for packets in the network that haven’t arrived yet) and sends an ACK confirmation packet, sequence number n + 1. The connection is now closed.
1. TCP Half-Close State
TCP provides the ability for one end of the connection to continue receiving data from the other end after it has finished sending. For example, the active party is in the fin_wait2 state.
2. TIME_WAIT State
The TIME_WAIT state is also known as the 2MSL wait state. Each specific TCP implementation must choose a Maximum Segment Lifetime (MSL). This is the longest time any segment can remain in the network before being discarded. Since TCP segments are transmitted as IP datagrams in the network, and IP datagrams have a TTL field that limits their lifetime, in practice, the limit on the TTL of IP datagrams is based on hops rather than timers. In the 2MSL waiting state, the socket (client IP and port, server IP and port) cannot be reused. However, in practice, a new connection request can reach a connection still in the TIME_WAIT state as long as the new sequence number is greater than the last sequence number of the previous connection.

Below is a complete process of TCP connection establishment, data transmission, and connection closure.
The process consists of 3 handshakes to establish the connection, one data transmission, and 4 handshakes to close the connection.

In abnormal situations, the server usually notifies via a reset packet, which is a TCP packet type set to rst.
1. Connection Timeout or Reaching a Nonexistent Port/Server
When the server is not open or there are network issues, a connection timeout may occur. The packet capture is as follows: The client attempts to connect three times, and sometimes the server will send a rst packet.
2. Abnormally Terminating a Connection
In TCP communication, if one party (e.g., A) closes the connection without sending a FIN packet due to some reason (such as sudden power failure), the other party (e.g., B) does not know that the other has closed the connection. When data is sent again, the party that closed the connection abnormally may return a rst packet to notify of the abnormal closure. If one party has closed or abnormally terminated the connection while the other party is still unaware, we call such a TCP connection half-open.
3. Simultaneous Opening
It is possible for two applications to actively open connections simultaneously. Each party must send a SYN, and these SYNs must be transmitted to each other. This requires each party to use a port known to the other as the local port. The state transition diagram for simultaneous opening differs from the normal three-way handshake, requiring 4 handshakes in this case. As shown:
4. Simultaneous Closure
We have previously discussed that one party (usually the client) sends the first FIN to actively close. It is also possible for both parties to execute an active close. The TCP protocol allows for such simultaneous closure. In the case of simultaneous closure, both parties enter the TIME_WAIT state, as shown:

Most TCP server processes are concurrent. When a new connection request arrives at the server, the server accepts this request and invokes a new process to handle this new client request.
1. Accepting Connection Request Queue
A concurrent server calls a new process to handle each client request, so the passive server must always be prepared to handle the next incoming connection request. This is the fundamental reason for using concurrent servers. However, it is still possible for multiple connection requests to arrive when the server is creating a new process, or when the operating system is busy handling higher-priority processes. How does TCP handle these incoming connection requests when the server is busy? TCP has a queue to temporarily store these connections – the incoming connection request queue. The processing method is as follows:
-
One side waiting for connection requests has a fixed-length connection queue, where connections have been accepted by TCP (i.e., the three-way handshake has been completed) but have not yet been accepted by the application layer. Note the distinction between TCP accepting a connection (putting it into this queue) and the application layer accepting the connection (removing it from the queue).
-
The application layer specifies the maximum length of this queue, usually referred to as the backlog value.
-
When a connection request (SYN) arrives, TCP uses an algorithm to determine whether to accept this connection based on the current number of connections in the queue. The backlog value indicates the maximum number of connections that TCP has accepted and are waiting for the application layer to accept.
-
If there is space in the connection queue for a new connection request, the TCP module will acknowledge the SYN and complete the connection establishment. At this time, the application layer may not yet know about the new connection; if the other party sends data, this data will be placed in the buffer queue.
-
If there is no space in the connection queue for a new connection request, TCP will ignore the received SYN and will not send back any segments (i.e., will not send back RST). If the application layer cannot promptly accept connections that have been accepted by TCP, these connections may fill the entire connection queue, leading to a timeout for the client’s active opening.

Face to Java Issue 171: Is SimpleDateFormat Thread-Safe?
The Road to God Issue 014: In-Depth Study of Enums in Java.
– MORE | More Exciting Articles –
-
How to Write a Programmer-Style Cultivation Novel?
-
In-Depth Understanding of Fail-Fast and Fail-Safe in Java
-
How to Explain Concurrency and Parallelism to My Girlfriend
-
Awesome! Teach You to Output HelloWorld in Nine Languages on JVM
If you have reached this point, it means you like this article.
Please long press the QR code to follow Hollis

Sharing in Moments is the greatest support for me.