Skip to content
TCP Header & Connection State
step 1/5

Reading — step 1 of 5

Read

~1 min readUDP & TCP Basics

TCP Header & Connection State

TCP (RFC 793) header is 20 bytes minimum:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  HL   |  Reserved |U|A|P|R|S|F|         Window               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Checksum              |        Urgent Pointer         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                  Options (variable, 0 to 40 bytes)            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Flags: URG, ACK, PSH, RST, SYN, FIN. Combinations matter.

Connection states:

  • CLOSED → LISTEN (server bind+listen).
  • LISTEN → SYN_RCVD (got SYN, send SYN+ACK).
  • SYN_SENT → ESTABLISHED (client got SYN+ACK, send ACK).
  • ESTABLISHED ↔ CLOSE_WAIT / FIN_WAIT_1 / FIN_WAIT_2 / TIME_WAIT.
  • TIME_WAIT for ~2*MSL (2 minutes) to absorb stragglers.

Per-connection state:

python

Most TCP code is state machine transitions on incoming segments.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…