/Learn
06 · Transport · IP protocol 6

TCP

Transmission Control Protocol

A reliable, ordered byte stream over an unreliable network.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a TCP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System designProtocols

Study with AI

Part 1 of 5

Basics

What the protocol is for, and the ideas it is built on.

1.1What it does

The protocol in plain words, before any bytes.

TCP turns IP’s unreliable packets into a dependable stream. It numbers every byte, acknowledges what arrives, resends what is lost, reorders what comes out of order and slows down when the network is congested.

Before any data flows, the two sides agree on starting sequence numbers with a three-way handshake: SYN, SYN-ACK, ACK. Closing uses FIN in each direction.

1.2Key ideas

The handful of ideas everything else follows from.

Sequence and acknowledgement

They count bytes, not segments. An ACK of N means “everything before byte N has arrived”.

Flow control

The receiver advertises a window, the buffer space it has left. The sender may not exceed it.

Congestion control

The sender grows its sending rate until loss or delay appears, then backs off (slow start, congestion avoidance, CUBIC, BBR).

Retransmission

Timeouts and duplicate ACKs trigger resends. SACK reports exactly which ranges arrived.

Head-of-line blocking

One lost segment stalls everything behind it. This is a main motivation for QUIC.

Closing

Each side closes its direction with FIN. TIME_WAIT keeps the port reserved so stray packets cannot confuse a new connection.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why is the initial sequence number random?
  2. What does an acknowledgement number mean?

The answers are in part 4.

Part 3 of 5

Detailed answer

Watch a real exchange step by step, then take each message apart field by field.

3.1Follow the packets

Step through the exchange between the two sides.

Client
Server
SYN, seq = x
SYN-ACK, seq = y, ack = x + 1
ACK, ack = y + 1
Data, seq = x + 1
ACK, ack = x + 1 + len
Step 1 of 5

The client picks a random initial sequence number and proposes options such as MSS and window scale.

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

First step of the three-way handshake, from client to server. No payload.

0000c93a01bb9e1f2a3000000000a002faf0·:····*0········
001038ff0000020405b40402080a4f1c3a218···········O·:!
00200000000001030307········
FieldSource port
Length2 bytes
Decimal51514

51514, an ephemeral client port.

FieldOffsetBytesHex
Source port02c9 3a
Destination port2201 bb
Sequence number449e 1f 2a 30
Acknowledgement number8400 00 00 00
Data offset121a0
Flags13102
Window142fa f0
Checksum16238 ff
Urgent pointer18200 00
Option: MSS20402 04 05 b4
Option: SACK permitted24204 02
Option: Timestamps261008 0a 4f 1c 3a 21 00 00 00 00
Option: NOP36101
Option: Window scale37303 03 07

3.3Decode it in code

A small decoder, tested against the exact bytes shown above.

Decoding a TCP header

This reads the SYN on this page, including its options.

tcp.tsTypeScript
1const FLAGS = ["FIN", "SYN", "RST", "PSH", "ACK", "URG"];2 3export function parseTcp(b: Uint8Array) {4  const view = new DataView(b.buffer, b.byteOffset, b.byteLength);5  const headerLength = (b[12] >> 4) * 4; // data offset counts 32-bit words6  const options: Record<string, number | boolean> = {};7  for (let i = 20; i < headerLength; ) {8    const kind = b[i];9    if (kind === 0) break; // end of options10    if (kind === 1) { i++; continue; } // NOP, used as padding11    if (kind === 2) options.mss = view.getUint16(i + 2);12    if (kind === 3) options.windowScale = b[i + 2];13    if (kind === 4) options.sackPermitted = true;14    if (kind === 8) options.timestamp = view.getUint32(i + 2);15    i += b[i + 1];16  }17  return {18    sourcePort: view.getUint16(0),19    destinationPort: view.getUint16(2),20    seq: view.getUint32(4),21    ack: view.getUint32(8),22    headerLength,23    flags: FLAGS.filter((_, bit) => b[13] & (1 << bit)),24    window: view.getUint16(14),25    options,26  };27}28 29/** SYN and FIN each use up one sequence number, even with no data. */30export function nextSeq(seq: number, payload: number, flags: string[]): number {31  const extra = flags.includes("SYN") || flags.includes("FIN") ? 1 : 0;32  return (seq + payload + extra) >>> 0; // sequence numbers wrap at 2^3233}
33 linesUTF-8TypeScript
(b[12] >> 4) * 4
The data offset says where options end.
kind === 1
NOP is one byte with no length. Every other option has a length byte.
nextSeq
SYN and FIN each use a sequence number, which is why the SYN-ACK acknowledges seq + 1.
>>> 0
Sequence numbers wrap at 2^32.

Tested by npm run test:modules.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why is the initial sequence number random?
To stop attackers guessing it and injecting packets, and to avoid confusion with old connections on the same ports.
What does an acknowledgement number mean?
The next byte the receiver expects. Everything before it has arrived.
Part 5 of 5

Extra depth

The mistakes people make with this protocol in real systems.

5.1Watch out for

  • Many short connections spend most time in handshakes. Reuse them.
  • A SYN flood fills the server’s half-open queue. SYN cookies defend against it.
  • Nagle’s algorithm and delayed ACKs together can add 40 ms stalls for small writes.
← DNSTLS →
AI Engineer · Protocol Explorer