TCP Handshake
The TCP handshake is the three-message exchange (SYN, SYN-ACK, ACK) used for the normal active/passive TCP open. Each side picks an initial sequence number (ISN) and confirms receipt of the other side's ISN, so both endpoints agree on where byte numbering starts. This walk-through carries no application data on the handshake segments, though TCP allows it.
Think of two pen pals starting a numbered correspondence: you write that your letters start at 1000, your pen pal replies expecting 1001 next while starting theirs at 5000, and you confirm theirs. Until both sides hold confirmation of their own starting number, neither can rely on message numbering in both directions.
How the TCP Handshake Works
Before the first segment moves, the two endpoints are in asymmetric states: the server sits in LISTEN, waiting passively, and the client is CLOSED with no connection state. The client makes the first move. It picks an ISN (here 1000), enters SYN-SENT to record the active attempt, and sends a segment with the SYN flag and seq=1000. SYN stands for synchronize: it asks the peer to synchronize its byte numbering with the client's starting value.
When the server reads that SYN, it allocates connection state and enters SYN-RECEIVED, a half-open state where only one direction has been initialized. Its reply handles two tasks in one segment: it acknowledges the client's SYN with ack=1001, and it proposes its own numbering with a SYN carrying seq=5000. The acknowledgment is 1001 rather than 1000 because a SYN consumes one sequence number even though it carries no payload, and an ACK always names the next sequence number expected.
The client reads the SYN-ACK and enters ESTABLISHED, since its own SYN has been acknowledged. The server's SYN still needs confirmation, so the client sends the final ACK with seq=1001, ack=5001. When the server reads that ACK, it enters ESTABLISHED as well. Both endpoints now hold the peer's starting number and confirmation of their own.
The client enters ESTABLISHED after receiving the SYN-ACK; the server enters it after receiving the final ACK. Between those events, the client considers the connection established while the server is still waiting for confirmation.
TCP Handshake Step by Step
The default scenario runs the normal handshake with clientIsn = 1000 and serverIsn = 5000:
- Init. Client is
CLOSED; server isLISTEN. No shared sequence context exists yet. - Client chooses its ISN: 1000. This number is fixed for the whole connection attempt.
- Client enters
SYN-SENT. State changes locally, before the server has seen anything. - Client sends
SYN seq=1000. The first segment leaves the client. - Server reads the arriving SYN. The segment arrives before the server changes state.
- Server enters
SYN-RECEIVED. The connection is now half-open, awaiting the final ACK. - Server computes
ack=1001. The SYN consumed sequence number 1000, so the next expected is 1001. - Server chooses its own ISN: 5000. Its SYN will consume one sequence number too.
- Server sends
SYN-ACK seq=5000, ack=1001. One segment carries both the server's SYN and its acknowledgment of the client's SYN. - Client reads the SYN-ACK. Its own SYN is now acknowledged.
- Client enters
ESTABLISHED. The client is ready; the server is still waiting. - Client computes
ack=5001to acknowledge the server's SYN, applying the same next-expected-number rule in the reverse direction. - Client sends the final
ACK seq=1001, ack=5001. - Server reads the final ACK. It stays in
SYN-RECEIVEDuntil it accepts the segment. - Server enters
ESTABLISHED. Both state machines now agree. - Done. The connection can carry application data.
On the canvas, each endpoint is a vertical lifeline with state badges stacking downward as transitions happen: CLOSED → SYN-SENT → ESTABLISHED on the client side, and LISTEN → SYN-RECEIVED → ESTABLISHED on the server side. Each segment travels between the lifelines as a card showing its flags and its primary seq or ack value while the top inspector displays both fields, taking a longer beat than local bookkeeping steps. Delivered segments fade into a dim trace carrying both seq and ack so the segment currently in flight stands out.
Why Three Messages?
Two messages cannot establish reliable numbering in both directions, and four would waste an extra one-way message. Reliable here means acknowledged: the client's SYN proposes numbering for the client→server direction and the server's ACK confirms it; the server's SYN proposes numbering for the server→client direction and the client's ACK confirms it. That requires four logical actions, but the server piggybacks its ACK and SYN into one SYN-ACK segment, collapsing four messages into three.
Without the third message, the server's SYN goes unconfirmed. The server cannot distinguish a client that accepted the connection from one that vanished, and it would remain in SYN-RECEIVED holding state for a peer that may no longer exist. The final ACK lets the server commit to ESTABLISHED.
Edge Cases
The Lost SYN-ACK preset replays the same connection with one failure injected: the server's SYN-ACK is lost in transit. The trace diverges after step 9 above.
The client remains in SYN-SENT without a reply. When its retransmission timer fires, it resends the same SYN with seq=1000, keeping the original ISN rather than starting a new attempt. Connection identification also uses endpoint addresses and ports, so changing the ISN does not automatically cause the server to allocate a second connection on the same tuple, though it would no longer retransmit the original SYN. Because the server is already in SYN-RECEIVED, it recognizes the duplicate SYN, allocates no new state, and resends its SYN-ACK with the same seq=5000, ack=1001. Once that segment arrives, the handshake completes as in steps 10–16. Packet loss adds latency rather than corrupting state because the sequence numbers make retransmission idempotent.
Common Mistakes
- Reading the ACK number as "the byte I just received." An ACK names the next sequence number expected from the peer. Acknowledging
seq=1000producesack=1001, and acknowledgingseq=5000producesack=5001. - Forgetting that a SYN consumes a sequence number. It carries no data, yet
ack = seq + 1. Missing this makes every sequence number in the trace appear off by one. - Assuming both sides become
ESTABLISHEDat the same moment. The client commits on receiving the SYN-ACK; the server commits one segment later, on receiving the final ACK. The half-openSYN-RECEIVEDwindow in between is what SYN-flood attacks exploit at scale. - Treating a retransmitted SYN as a new connection. The same connection attempt retransmits the same SYN and ISN, and the connection is also identified by its endpoint tuple. The retry repeats the unacknowledged segment rather than restarting the negotiation.
- Thinking the ISNs are always small round numbers. The values
1000and5000here are chosen for readability. Real TCP stacks pick hard-to-predict ISNs so stale segments rarely land in a live connection's sequence space and off-path forgers must guess; this lowers the risk without authenticating segments.
A Note on Simplification
This visualization omits TCP options, receive windows, SYN cookies, simultaneous open, and RTT-based timeout estimation so the three-segment state machine stays readable. The sequence numbers and state transitions follow the real protocol; the retransmission timer is simplified.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Premium players need paid access. Every Explain doc is free.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?