TCP shook hands three times and waved hands four times
TCP is a connection-oriented transport layer protocol. Before transmitting application data, both parties need to establish a connection through three handshakes; after the data transmission is over, the two-way connection is usually released through four message segments.
Relevant fields in TCP header

serial number seq
Serial number field occupied 32 bit. TCP numbers the data in the byte stream, and the sequence number in a message segment usually represents the first byte number of the data carried by the message segment.
When establishing a connection, both parties to the communication will select the initial serial number respectively.SYN and FIN Even if you don’t carry application data, each will consume a serial number.
confirmation number ack
Confirmation number field occupied 32 Bit that represents the next byte sequence number that the recipient expects to receive.
For example, if you have correctly received the other party’s serial number 100 to 199 , the confirmation number is usually 200。
ACK flag bit
ACK accounting for 1 bit. when ACK for 1 When the confirmation number field is valid. After the connection is established, this flag bit will be set in most TCP message segments.
Need to distinguish:
- lowercase
ackTypically represents the value of the confirmation number field. - capitalized
ACKRepresents the acknowledgement flag bit in the TCP header.
SYN flag bit
SYN The initial serial number used to establish a connection and synchronize both parties.
SYN=1、ACK=0: usually represents a connection request.SYN=1、ACK=1: Usually expresses agreement to establish a connection and confirms the other party’s request.
FIN flag bit
FIN Indicates that the sender has no data to send, and requests to close the connection in the current direction. TCP is a full-duplex protocol, so after one direction is turned off, data can still continue to be transmitted in the other direction.
three-way handshake

Suppose the client’s initial serial number is x, the initial serial number of the server is y。
First handshake: client sends SYN
The client sends a connection request to the server:
SYN=1, seq=xAfter sending, the client receives CLOSED enter SYN-SENT Status.
Second handshake: server send SYN+ACK
After receiving the client’s request, the server confirms the client’s serial number and sends its own initial serial number:
SYN=1, ACK=1, seq=y, ack=x+1After sending, the server receives LISTEN enter SYN-RECEIVED Status.
Third handshake: client sends ACK
After receiving the server’s response, the client sends a confirmation message:
ACK=1, seq=x+1, ack=y+1After the server receives the confirmation, both parties enter ESTABLISHED Status, application data can be transmitted.
Pure in the third handshake ACK If the message does not carry data, it will not consume additional serial numbers.
Why do you need three handshakes?
The three handshakes mainly completed the following tasks:
- Confirm that the sending and receiving capabilities of both parties are basically normal.
- Synchronize the initial serial numbers of both parties.
- Avoid invalid old connection requests that directly cause the server to establish wrong connections.
- Negotiate TCP options such as maximum segment length and window expansion factor.
When there are only two interactions, the server cannot confirm whether the client has received the server’s initial serial number and connection confirmation, so a third confirmation is needed.
Wave four times

Here is an example of a client actively closing the connection. Suppose the client sends FIN The serial number of the time is u, the current serial number of the server is v。
The first wave: client sends FIN
The client indicates that it has no data to send:
FIN=1, ACK=1, seq=uclient enters FIN-WAIT-1 Status.FIN It consumes a serial number.
The second wave: the server confirms the client’s FIN
server receives FIN Send confirmation later:
ACK=1, seq=v, ack=u+1server enters CLOSE-WAIT Status, entered after the client receives confirmation FIN-WAIT-2 Status.
At this time, the client-to-server direction has been turned off, but the server-to-client direction can still continue to transmit data that has not yet been sent. This state is called semi-closed.
Third wave: server send FIN
After the server completes sending the remaining data, it requests to turn off its sending direction. Suppose the server serial number is w:
FIN=1, ACK=1, seq=w, ack=u+1server enters LAST-ACK Status, waiting for final confirmation from the client.
Fourth wave: The client confirms the server’s FIN
The client receives the server’s FIN Send confirmation later:
ACK=1, seq=u+1, ack=w+1client enters TIME-WAIT Status. The server enters after receiving this confirmation CLOSED Status.
Why does it usually take four waves?
Both transmission directions of TCP need to be turned off separately. Send by actively closing party FIN After that, the passive closing party may still have data to send, so it usually confirms the other party’s data separately first. FIN, wait until the remaining data is sent before sending your own FIN。
If the passive closing party receives FIN When there is no data to send, it may also be ACK with their FIN Merge into the same message segment. At this time, the number of message segments seen on the network may be less than four, but the connection release logic still includes closure and confirmation in both directions.
TIME-WAIT state
Active closings usually TIME-WAIT state waits for 2MSL。MSL Represents the maximum lifetime of a message segment in the network.
There are two main reasons for waiting:
- If the last one
ACKLost, passive shutdown will be reissuedFIN, the active closing party can still confirm again. - Allow delayed messages generated by the current connection to disappear in the network to avoid affecting new connections established later using the same quadruple.
TIME-WAIT It is an important mechanism for TCP to ensure reliable shutdown and is not equivalent to a program exception.
Summary of common status
| state | meaning |
|---|---|
LISTEN | The server listens for connection requests |
SYN-SENT | The client has sent a connection request |
SYN-RECEIVED | The server has received the request and awaits final confirmation |
ESTABLISHED | connection has been established |
FIN-WAIT-1 | The active closing party has sent FIN, waiting for confirmation |
FIN-WAIT-2 | The active closing party has received confirmation and is waiting for the other party’s FIN |
CLOSE-WAIT | The passive closing party has confirmed the other party’s FIN, wait for the app to close the connection |
LAST-ACK | The passive shutdown party has sent its own FIN, waiting for final confirmation |
TIME-WAIT | Active closing party waiting 2MSL |
CLOSED | connection has been closed |
If you enjoyed this, leave a comment~