A TCP of your own over a TUN device

: 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.