TCP 3-Way Handshake Simulation

Before exchanging application bytes, TCP establishes a reliable connection using a three-step handshake: SYN, SYN-ACK, and ACK. Both sides agree on initial sequence numbers and verify bidirectional reachability.

Interactive simulation

Client state
CLOSED
Server state
LISTEN
Client ISN (x)
100
Server ISN (y)
500
Timer
idle
Speed
1x
Client CLOSED
Server LISTEN
Step 0 of 3: Ready to initiate SYN

Legend

  • SYN segment (Client → Server)
  • SYN-ACK segment (Server → Client)
  • ACK segment (Client → Server)
  • Selected in transit (press Kill Selected)
  • Destroyed in transit
  • ESTABLISHED state

Event log

    How to use this simulation

    1. Press Send once to send the initial SYN (seq=x) from Client to Server. The client enters SYN-SENT.
    2. Press Send again once the server receives the SYN to send SYN-ACK (seq=y, ack=x+1), or let the simulation run and watch each stage unfold.
    3. Click any in-flight segment to select it, then press Kill Selected to simulate packet loss and watch the retransmission timer fire.
    4. Use Step to jump to the next event, Slower (down to 0.25x) and Faster (up to 4x) to adjust speed, and Reset to generate new initial sequence numbers.

    1. Step 1: Client sends SYN (seq = x)

    A TCP connection starts when the active opener (typically the client) sends a segment with the SYN (Synchronize) control flag set and picks a random 32-bit Initial Sequence Number, denoted here as x. The client moves from CLOSED to SYN-SENT and starts a retransmission timer.

    2. Step 2: Server replies with SYN-ACK (seq = y, ack = x + 1)

    When a listening server receives the SYN, it allocates socket resources, chooses its own Initial Sequence Number y, moves to SYN-RCVD, and transmits a SYN-ACK segment. Setting ack = x + 1 confirms receipt of the client's SYN (which consumes one sequence number) and tells the client what byte sequence the server expects next.

    3. Step 3: Client sends final ACK (seq = x + 1, ack = y + 1)

    When the client receives SYN-ACK, it verifies that ack equals x + 1, transitions to ESTABLISHED, and sends back an ACK with ack = y + 1. Once the server receives that ACK, it also transitions to ESTABLISHED. At this point, full-duplex data transfer can begin in both directions.

    4. Why three steps instead of two?

    A two-way handshake is not enough because both peers must establish and confirm their own starting sequence numbers independently. In addition, a delayed or duplicate SYN from an old connection could trick a server into opening a stale connection if the client never confirms the server's SYN-ACK.

    5. Retransmissions and SYN flood risks

    If a SYN or SYN-ACK is lost in transit, the sender's retransmission timer expires and resends the segment. Because the server holds half-open state in SYN-RCVD while waiting for the final ACK, high volumes of unanswered SYN packets can exhaust connection queues—a classic denial-of-service pattern known as a SYN flood.

    Handshake message breakdown

    StepDirectionFlagsSequence & AckPurpose
    1Client → ServerSYNSEQ = xRequest connection and advertise client ISN x.
    2Server → ClientSYN, ACKSEQ = y, ACK = x + 1Accept request, advertise server ISN y, and confirm x.
    3Client → ServerACKSEQ = x + 1, ACK = y + 1Confirm server ISN y; complete connection setup.

    Key takeaways

    • TCP uses a 3-way handshake (SYN, SYN-ACK, ACK) to synchronize sequence numbers for both directions.
    • SYN and FIN flags each consume one sequence number; pure ACK segments do not.
    • The client enters ESTABLISHED as soon as it sends the third segment (ACK), while the server enters ESTABLISHED when it receives that ACK.
    • Losing any segment in flight triggers a timeout and retransmission rather than corrupting connection state.

    Related topics

    Go-Back-N

    Sliding window ARQ with cumulative ACKs.

    Selective Repeat

    Sliding window ARQ with individual ACKs and buffering.

    TCP Teardown

    Graceful connection close with FIN and FIN-ACK.

    Coming soon

    Stop-and-Wait

    The simplest ARQ: one packet at a time.

    Coming soon

    Sliding Window

    Flow control and windowing fundamentals.

    Coming soon