NOTE

1.2 TCP Four-Way Handshake

What the TCP four-way connection termination is, how it works, why four handshakes are needed, and TIME_WAIT/CLOSE_WAIT states.

Computer NetworksCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is the Four-Way Handshake?

The so-called four-way handshake means that four packets need to be sent when TCP disconnects.

2. Four-Way Handshake Process

TCP Four-Way Handshake

At the beginning, both the client and server are in the ESTABLISHED state. Assume that the client starts closing the connection.

Memory aid: set FIN/ACK to 1. FIN comes with a SEQ Number, and ACK comes with an ACK Number. After sending, consider which state is entered.

  1. First handshake

The client sends a segment.

Set FIN to 1 and SEQ Number to X.

After sending, the client enters the FIN_WAIT_1 state.

  1. Second handshake

The server replies with a segment.

Set ACK to 1 and ACK Number to x+1.

After the server sends it, it enters the CLOSE_WAIT state.

After the client receives this acknowledgement packet, it enters the FIN_WAIT_2 state.

  1. Third handshake

The server sends a segment.

Set FIN to 1 and SEQ Number to Y.

After sending, the server enters the LAST_ACK state.

  1. Fourth handshake

The client replies with a segment.

Set ACK to 1 and ACK Number to Y+1.

After sending, the client enters the TIME_WAIT state and waits for possible requests to retransmit the ACK packet.

After the server receives this acknowledgement packet, it enters the CLOSED state.

After the client waits for a fixed period of time (2MSL), it enters the CLOSED state.

3. Why Are Four Handshakes Needed?

The first two handshakes disconnect the client’s data-sending channel. After it is disconnected, the server can still send messages to the client.

The last two handshakes disconnect the server’s data-sending channel.

4. Handshake States

4.1. TIME WAIT

TCP time wait.md

4.2. CLOSE WAIT

TCP close wait

5. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub