TCP shook hands three times and waved hands four times

Published 2026-07-30 20:19 Updated 2026-10-13 17:14 1138 words 6 min read ... Page views AI-reviewed An AI proof-read this post, but it still deserves a careful read. AI-rewritten Someone else wrote the original; AI helped me rewrite and polish it.

AI-reviewed An AI proof-read this post, but it still deserves a careful read.

This article introduces in detail the three-way handshake and four-way waving mechanism in TCP protocol. The three-way handshake is used to establish a connection, which ensures that the connection is reliably established by synchronizing the initial serial numbers of both parties and confirming the communication capabilities; the four-way handshake is used to close the connection. Since TCP is a full-duplex protocol, transmission in both directions needs to be turned off respectively to ensure that the data is sent completely and reliably released. The process involves the use of serial numbers, acknowledgement numbers, flag bits (SYN, ACK, FIN), as well as the role of TIME-WAIT status to ensure the reliability of connection closure and the stability of the network environment.

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

image-001
image-001

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 ack Typically represents the value of the confirmation number field.
  • capitalized ACK Represents 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

image-002
image-002

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=x

After 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+1

After 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+1

After 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

image-003
image-003

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=u

client 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+1

server 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+1

server 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+1

client 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 ACK Lost, passive shutdown will be reissued FIN, 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

statemeaning
LISTENThe server listens for connection requests
SYN-SENTThe client has sent a connection request
SYN-RECEIVEDThe server has received the request and awaits final confirmation
ESTABLISHEDconnection has been established
FIN-WAIT-1The active closing party has sent FIN, waiting for confirmation
FIN-WAIT-2The active closing party has received confirmation and is waiting for the other party’s FIN
CLOSE-WAITThe passive closing party has confirmed the other party’s FIN, wait for the app to close the connection
LAST-ACKThe passive shutdown party has sent its own FIN, waiting for final confirmation
TIME-WAITActive closing party waiting 2MSL
CLOSEDconnection has been closed

Written by AI-rewritten

If you enjoyed this, leave a comment~

Loading...