1. Home
  2. Computer Networks
  3. TCP 3-Way Handshake

TCP 3-Way Handshake

How two computers agree to start a reliable conversation — SYN, SYN-ACK, ACK — plus data acknowledgements and the 4-way close, as a 3D sequence diagram.

Interactive 3DBeginner11 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · ←→ step

What's happening

Pseudocode

    Try this in the 3D model

    • Run the 3-way handshake. How is each ACK number calculated?
    • Choose Handshake + data transfer. After 100 bytes starting at 101, what ACK does the server send?
    • Choose 4-way connection close and follow the state labels on both sides.

    Why a handshake?

    TCP (Transmission Control Protocol) promises reliable, ordered delivery over an unreliable network. To do that, both sides number every byte they send with sequence numbers, and acknowledge what they receive with ACK numbers.

    Before sending data, the two computers must agree on their starting sequence numbers. That agreement is the 3-way handshake.

    The three steps

    Client                                    Server (LISTEN)
      |  ---- SYN, seq = x ----------------->   |      client: SYN-SENT
      |  <--- SYN-ACK, seq = y, ack = x + 1 --  |      server: SYN-RECEIVED
      |  ---- ACK, seq = x + 1, ack = y + 1 -->  |      both: ESTABLISHED
    1. SYN — “I want to connect; my first sequence number is x.”
    2. SYN-ACK — “OK. I got x (I expect x + 1 next), and my first number is y.”
    3. ACK — “Got y. Let’s go.”

    Starting numbers are random, which protects against confusing old, delayed packets with a new connection (and against some attacks).

    Why not two steps?

    With only SYN and SYN-ACK, the server would never know whether its own starting number y reached the client. Each side needs to send its number and see it acknowledged — that takes three messages.

    Data transfer

    Every data segment carries a sequence number; the receiver replies with an ACK = next byte expected. If the client sends 100 bytes starting at 101, the server answers ack = 201. Missing ACKs trigger retransmission. To avoid waiting for every single ACK, TCP keeps several segments in flight using a sliding window.

    Closing: the 4-way handshake

    Each direction is closed separately:

    1. Client → FIN (I’m done sending) → FIN-WAIT-1
    2. Server → ACK → server CLOSE-WAIT, client FIN-WAIT-2
    3. Server → FIN (I’m done too) → LAST-ACK
    4. Client → ACK → client waits in TIME-WAIT (to catch a lost final ACK), then both CLOSED

    Key TCP states

    State Meaning
    LISTEN Server waiting for connections
    SYN-SENT / SYN-RECEIVED Handshake in progress
    ESTABLISHED Connection open — data can flow
    FIN-WAIT, CLOSE-WAIT, LAST-ACK Closing
    TIME-WAIT Waiting a short while after closing

    See it on your own computer

    # Linux / macOS: list TCP connections and their states
    netstat -an | grep -E "LISTEN|ESTABLISHED"

    A tiny TCP client in Python — the handshake happens inside connect():

    import socket
    s = socket.create_connection(("example.com", 80))   # SYN, SYN-ACK, ACK happen here
    s.sendall(b"GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")
    print(s.recv(200))
    s.close()                                           # FIN / ACK exchange

    TCP vs UDP

    TCP UDP
    Connection setup 3-way handshake None
    Reliability & order ✅ ❌
    Speed / overhead Higher overhead Very light
    Used for Web, email, file transfer Video calls, gaming, DNS

    Common mistakes

    • Forgetting the +1 in ACK numbers (a SYN or FIN counts as one sequence number).
    • Mixing up which side sends the SYN-ACK (always the side that received the SYN).
    • Thinking closing also takes three messages — it usually takes four.

    Complexity at a glance

    Case / operationTimeWhy
    Messages to open a connection3SYN, SYN-ACK, ACK.
    Messages to close4FIN, ACK, FIN, ACK.
    Delay before data can flow1 round-trip time

    Quick check

    Test yourself — pick an answer to see if you got it.

    1. What is the correct order of the TCP handshake?

    2. The client's SYN has seq = 100. What ack number does the server's SYN-ACK carry?

    3. Why does TCP need a handshake but UDP does not?

    4. Which state does a server wait in before any client connects?

    Saved only in this browser — no account needed.
    Spotted a mistake or a bug in the 3D model?

    Report a mistake

    in TCP 3-Way Handshake. Thank you — every report makes the lesson better for the next reader.

    We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.