Skip to content

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

    Computer Basics / Networking 2: TCP, UDP and Ports

    Quick Reference

    Glossary and cheat sheet for Networking 2: TCP, UDP and Ports — the page to reread before an interview.

    Extra: Reference7 min read

    Glossary#

    TermPlain meaningChapter
    port16-bit number for which program on a machine1
    well-known / ephemeral portfixed server port (443) / random client port1
    five-tupleprotocol + source IP/port + destination IP/port = one connection1
    socketkernel object a program uses for networking; a file descriptor2
    bind / listen / accept / connectattach to address:port / start accepting / take one caller / dial out2
    0.0.0.0 vs 127.0.0.1 bindingreachable from anywhere vs only this machine2
    UDP / datagramno-guarantee transport / one UDP message3
    TCP / segmentreliable ordered byte-stream transport / one TCP chunk4
    byte streamdata with order but no message boundaries4
    three-way handshakeSYN, SYN-ACK, ACK5
    sequence numberposition of a segment's first byte in the stream5, 6
    RTTround trip time5
    SYN flood / SYN cookieshalf-open connection attack / the defence5
    refused vs timeoutnothing listening (RST) / nothing replied (often a firewall)5, 11
    ACK (cumulative)"I have everything before byte n"6
    retransmission / RTO / fast retransmitresending / how long to wait / resend on 3 duplicate ACKs6
    flow control / receive windowdon't overwhelm the receiver / its free space7
    congestion control / congestion windowdon't overwhelm the network / the sender's guess7
    slow startbegin small, double each RTT7
    FIN / RSTpolite close / abrupt kill ("connection reset by peer")8
    LISTEN / ESTABLISHED / TIME_WAIT / CLOSE_WAITconnection states (CLOSE_WAIT piling up = our bug)8
    TCP keep-aliveprobes to detect dead idle connections8
    head-of-line blockingone loss stalls everything behind it9
    QUICUDP-based transport with independent streams; carries HTTP/39
    NAT / NAT tablerewriting private source address+port to public / the mapping10
    port forwardingfixed rule sending a public port to a private device10
    CGNATISP-level NAT shared by many customers10
    NAT gatewaycloud service letting private servers call out10
    firewall (stateful / stateless)allow/deny rules (remembers connections / doesn't)11
    security group / network ACLAWS stateful per-server / stateless per-subnet rules11
    least privilegeopen only exactly what's needed11
    latency / bandwidthtime per trip / data per second12
    keep-alive (HTTP) / connection reusemany requests over one connection12
    connection poolready-made open connections shared by handlers12

    Cheat Sheet#

    Ports to know

    Output
      22 SSH   53 DNS   80 HTTP   443 HTTPS   5432 Postgres   3306 MySQL   6379 Redis   27017 MongoDB
      dev: 8000 FastAPI · 3000 React/Next · 5173 Vite         privileged (Linux, root only): 0–1023

    TCP in one screen

    Output
      open:   SYN → SYN-ACK → ACK            (1 RTT before data)
      data:   sequence numbers + cumulative ACKs; resend on timeout or 3 dup ACKs
      speed:  min(receive window, congestion window); slow start doubles per RTT
      close:  FIN/ACK each way (polite) · RST (abrupt)
      states: LISTEN · ESTABLISHED · TIME_WAIT (normal, 60 s on Linux) · CLOSE_WAIT (our bug)

    Symptoms → likely cause

    Output
      "connection refused"          host reachable, nothing listening on that port
      "connection timed out"        firewall/security group dropping, or no route
      "connection reset by peer"    peer/LB/NAT killed it — often an idle pooled connection
      "address already in use"      another process listens on that port (lsof -i :PORT)
      works locally, not remotely   bound to 127.0.0.1 — bind 0.0.0.0
      thousands of CLOSE_WAIT       our code isn't closing sockets

    Round-trip maths

    Output
      new HTTPS connection ≈ 3 RTT to first response (TCP + TLS 1.3 + request) · reused ≈ 1 RTT
      time ≈ round trips × RTT + size ÷ bandwidth       → for small data, latency dominates

    Commands (macOS · Linux)

    Output
      who listens on a port    lsof -i :8000              ·  ss -tlnp
      all connections + state  netstat -an | grep 443     ·  ss -tan
      raw TCP client/server    nc -l 9000   /   nc localhost 9000
      test a port is open      nc -vz 13.235.10.20 443
      see HTTP + connection    curl -v https://example.com

    Try This Yourself#

    1. Handshake with netcat. Run nc -l 9000 in one terminal and nc localhost 9000 in another. In a third, run netstat -an | grep 9000 and find the LISTEN socket and the two ESTABLISHED ends (one for each direction of the conversation, with different ports). Close the client with Ctrl+C, run the command again, and look for TIME_WAIT.

    2. Refused vs timeout. Run nc -vz localhost 9999 (nothing listening → refused, instant). Then run nc -vz -G 5 10.255.255.1 80 (an unreachable private address → times out after 5 seconds; on Linux use -w 5 instead of -G 5). Feel the difference.

    3. See connection reuse. Run curl -v https://example.com https://example.com (the same URL twice in one command). In the output, look for "Re-using existing connection" on the second request — no second handshake.