Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 2: TCP, UDP and Ports

    The Three-Way Handshake

    SYN flood: an attacker sends huge numbers of SYNs and never completes the handshake, filling the

    Chapter 5 of 124 min read

    The Plain Picture#

    A phone call starts like this:

    1. You: "Hello, can you hear me?"

    2. Friend: "Yes, I can hear you — can you hear me?"

    3. You: "Yes!"

    Only now do you start talking. Both sides know that both directions work.

    The Real Names#

    TCP opens a connection with three packets — the three-way handshake:

    1. SYN ("synchronize"): client → server. "I want to connect. My byte numbering starts at x."

    2. SYN-ACK: server → client. "OK (ACK = acknowledge x). My numbering starts at y."

    3. ACK: client → server. "Got it (acknowledge y)."

    The starting numbers are initial sequence numbers. They're chosen randomly, for security (so an attacker can't guess them and inject fake data) and so old stray packets from a previous connection aren't mistaken for new ones.

    Under the Hood#

       Client (laptop)                                     Server (13.235.10.20:443)
       192.168.1.37:52341                                  state: LISTEN
            │                                                     │
            │ ── SYN  seq=1000 ─────────────────────────────────▶ │
            │                                                     │
            │ ◀─────────────── SYN-ACK  seq=5000, ack=1001 ────── │
            │                                                     │
            │ ── ACK  ack=5001 ─────────────────────────────────▶ │
            │                                                     │
       ESTABLISHED                                           ESTABLISHED
            │ ── data: "GET /notes/priya" (seq=1001) ───────────▶ │

    ("ack = 1001" means "I've received everything up to byte 1000; send me 1001 next". The SYN itself counts as one byte of numbering, which is why the ack is x + 1.)

    The cost: one round trip before any data#

    A round trip time (RTT) is the time for a packet to go there and a reply to come back. The client can't send its request until the SYN-ACK has come back — so every new TCP connection costs one RTT before any useful data.

    Output
      Bangalore → Mumbai server:   RTT ≈ 20 ms     → 20 ms lost per new connection
      Bangalore → US server:       RTT ≈ 220 ms    → 220 ms lost per new connection

    And HTTPS then adds encryption setup on top — at least one more RTT (Networking 3: DNS, HTTP and HTTPS). This is why reusing connections matters so much (Ch 12).

    Gotchas

    • SYN flood: an attacker sends huge numbers of SYNs and never completes the handshake, filling the server's table of half-open connections. Kernels defend with SYN cookies (they avoid storing state until the final ACK arrives).

    • Connection timeout vs connection refused:

      • Refused = the SYN reached the machine, but nothing listens on that port; the machine replied with a reset (Ch 8). Instant.

      • Timeout = no reply at all, usually a firewall silently dropping the SYN (Ch 11) or no route. Slow — the client waits and retries. Knowing the difference halves your debugging time.

    Check yourself

    4 questions
    1

    Name the three packets of the handshake, in order.

    Show answer

    SYN, SYN-ACK, ACK.

    2

    Why are initial sequence numbers random?

    Show answer

    Security (hard to guess and inject fake data) and to avoid confusion with packets from old connections.

    3

    How much time does a new TCP connection cost before data can be sent?

    Show answer

    One round trip time.

    4

    "Connection refused" vs "connection timed out" — what does each tell you?

    Show answer

    Refused: the host is reachable but nothing is listening on that port. Timed out: nothing replied — often a firewall dropping packets, or a routing problem.