TCP three-way handshake

How two hosts agree to talk, and how they stop

sourceRFC 9293

TCP opens a connection with three messages and closes it with four. Sequence numbers keep both sides in step.

A

Three-way handshake

open
ClientServerLISTENSYN, seq=x1SYN_SENTSYN+ACK, seq=y, ack=x+12ACK, ack=y+13ESTABLISHED
B

Why three messages

  1. The server learns that the client can send.
  2. The client learns that the server can send and receive.
  3. The server learns that the client can receive.
Two is not enough

An old duplicate SYN can arrive late. With two messages, the server opens a useless connection for it.

C

State changes

simplified
passive openget SYN / send SYN+ACKactive open / send SYNget SYN+ACK / send ACKget ACKCLOSEDLISTENSYN_RCVDSYN_SENTESTABLISHED
D

Flags

Flag Job In handshake
SYN Start a connection steps 1, 2
ACK Confirm receipt steps 2, 3
FIN Stop sending not used
RST Reset at once on error