Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 14 · From Scratch · Systems from scratch · about 17 min to read

Play it as a journeyTutor comes with a plan

A TCP of your own over a TUN device

TCP Handshake
TCP Handshake · Open board

: the three messages that open a TCP connection. You will write the server side of this picture yourself, in Python, and watch a real client talk to it.

Every program that opens a web page uses TCP, and almost none of them ever see it. You call connect() and the operating system does the rest. So when a connection hangs or slows down, you are guessing about code you have never read. Here is what a real connection looks like when you ask the kernel to show it. This is a capture on this machine on 2026-11-28, one line per packet:

10.0.0.1.45856 > 10.0.0.2.7000: Flags [S],  seq 338138005, win 64240
10.0.0.2.7000 > 10.0.0.1.45856: Flags [S.], seq 3961976849, ack 338138006, win 65535
10.0.0.1.45856 > 10.0.0.2.7000: Flags [.],  ack 3961976850, win 64240

By the end of this lesson you will be able to write the program that sent the second line: a TCP server of about 100 lines that answers a real client, numbers its bytes, resends what gets lost, and checks every byte arrived.

The history (optional)

TCP was written down in RFC 793, edited by Jon Postel and published in September 1981. It says each side must “synchronize on each other’s initial sequence numbers” before a connection counts as open. It also says where the starting number should come from: a 32 bit clock whose lowest bit changes about every 4 microseconds. The RFC says the numbers repeat after about 4.55 hours. The sum is below, and it gives a slightly different answer.

Before you start. TCP vs UDP, and the 3-way handshake gave you the three-message handshake and why a random starting number stops a forger. Build it: the page that gets through (stop-and-wait, then a window) gave you numbered pages, an ack for each, a timeout that resends, and a window that keeps several pages on the road. Build it: only the lost page (selective repeat, loss and reordering) showed that a late page looks the same as a lost page. Here the pages become real TCP segments, and you need Python 3, Linux, and sudo once to make the device.

Lesson 106 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • A TUN device: a network cable that ends in your program
  • Sequence numbers: every byte has a number
  • The handshake, in your own code
  • Sending a stream: acks, a window and a timer
  • The full program
  • The state machine we skipped
  • The timer RFC 793 describes
  • What the kernel does that we do not
  • Break it on purpose

Terms it explains: TUN device, sequence number, acknowledgment number, window, retransmit timer, duplicate ack.

Lab

The pipe is 100 Mbps and the file is 1 MB. Why does the round-trip time decide how long it takes?

Drag How far away the server is out to the satellite, then press Play next flight and watch The clock.

Open The TCP handshake

Related paper

Review From Scratch