diagram.mmd — sequence
TCP Three-Way Handshake sequence diagram

The TCP three-way handshake is the connection establishment procedure of the Transmission Control Protocol, where a client and server exchange three messages — SYN, SYN-ACK, and ACK — to synchronize sequence numbers and establish a reliable full-duplex connection.

TCP (RFC 793) is the foundational transport protocol for most internet traffic: HTTP, HTTPS, SSH, SMTP, and many others rely on it. Unlike UDP, TCP guarantees ordered delivery, retransmission of lost packets, and flow control.

SYN (Synchronize): The client sends a TCP segment with the SYN flag set and an Initial Sequence Number (ISN) — a randomly chosen 32-bit number. The ISN is random to prevent TCP sequence prediction attacks.

SYN-ACK (Synchronize-Acknowledge): The server acknowledges the client's ISN (ACK = client_ISN + 1) and sends its own ISN. Both SYN and ACK flags are set. At this point the server has allocated a TCB (Transmission Control Block) for the connection but it isn't fully established yet.

ACK (Acknowledge): The client acknowledges the server's ISN (ACK = server_ISN + 1). The connection is now ESTABLISHED on both sides. Data can flow immediately in the client's ACK segment (TCP Fast Open) or in subsequent segments.

This handshake adds exactly 1 RTT of latency before any application data can be sent. This cost is paid before TLS negotiation (TLS Handshake), meaning a full HTTPS connection establishment costs 2–3 RTTs total.

Connection teardown uses a four-way exchange: FIN from the closing side, ACK from the other, FIN from the other, and final ACK. TCP's TIME_WAIT state holds the connection open for 2×MSL (typically 60–120 seconds) to handle delayed duplicate packets.

QUIC (used in HTTP/3) eliminates the TCP handshake by building its own reliability and ordering over UDP, integrating TLS 1.3 into a single 1-RTT handshake.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

The TCP three-way handshake is the connection establishment procedure used by the Transmission Control Protocol. A client and server exchange three segments — SYN, SYN-ACK, and ACK — to synchronize sequence numbers and create a reliable, full-duplex connection before any application data is sent.
The client sends a SYN segment with a randomly chosen Initial Sequence Number. The server replies with SYN-ACK, acknowledging the client's ISN and including its own ISN. The client sends a final ACK confirming the server's ISN. After this exchange, both sides have a fully established TCP connection and data can flow immediately.
TCP uses a four-step teardown. The closing side sends a FIN; the other side ACKs it. The other side then sends its own FIN; the original closer ACKs it. Both sides then close. The initiating side enters TIME_WAIT for 2×MSL (typically 60–120 seconds) to handle any delayed duplicate packets still in transit.
The TCP handshake adds exactly one round-trip time (RTT) of latency before any application data can be sent — typically 10–100ms depending on geographic distance. TLS negotiation adds another 1–2 RTTs on top. QUIC eliminates the TCP handshake cost by integrating transport and TLS negotiation into a single 1-RTT exchange over UDP.
mermaid
sequenceDiagram participant Client participant Server note">Note over Client: State: CLOSED note">Note over Server: State: LISTEN Client->>Server: SYN (seq=1000, SYN=1) note">Note over Client: State: SYN_SENT note">Note over Server: Allocate TCB Server-->>Client: SYN-ACK (seq=5000, ack=1001, SYN=1, ACK=1) note">Note over Server: State: SYN_RECEIVED Client->>Server: ACK (seq=1001, ack=5001, ACK=1) note">Note over Client: State: ESTABLISHED note">Note over Server: State: ESTABLISHED note">Note over Client,Server: Data transfer begins Client->>Server: Data (seq=1001) Server-->>Client: ACK (ack=1001+data_len) note">Note over Client,Server: Connection Teardown Client->>Server: FIN (seq=N) Server-->>Client: ACK (ack=N+1) Server-->>Client: FIN (seq=M) Client->>Server: ACK (ack=M+1) note">Note over Client: TIME_WAIT (2xMSL)
Copied to clipboard