TCP vs UDP, and the 3-way handshake a show in 8 chapters
Goal: send what matters over a network that promises nothing, and know who is on the line
1 of 8 Your guess
Before we start
You send a message to a friend across town. Between you sit many machines that pass it along, and none of them promises anything. A message can vanish, arrive late, or arrive claiming to be from someone it is not.
Which picture is closest to how a message gets across reliably anyway?
Pick the closest. You find out which fits at the end.
2 of 8 The problem
Christmas Day, 1994
On December 25, 1994, the computers of a security researcher named Tsutomu Shimomura, working in San Diego, were broken into. The attack came from a machine in San Francisco, later traced to a well-known intruder, Kevin Mitnick.
No password was cracked by trial and error. What made the case famous was stranger: his computers were made to believe a message came from a machine they already trusted, when it did not.
To see how that can work, ask what a network actually promises when you send something across it. Does it arrive? Does it arrive in order? Is it from who it says?
The honest answer to all three is the same: almost nothing.
The next chapter shows why the third one is the weak spot.
3 of 8 The idea
A packet is a postcard
A message crossing the internet is cut into chunks. Each chunk carries its own address on the outside, like a postcard, not a sealed truck with a driver who knows the whole route. One such chunk is a packet.
Your computer hands each packet to the network. Routers, machines that pass things along, move it toward the address. It either arrives or it does not, and nobody promises to tell you if it got lost.
A postcard's return address is only whatever the sender wrote in the corner. Nothing checks it. Write someone else's name there and the card is still delivered, and the receiver cannot tell from the card alone.
That is the weak spot the Christmas Day attack used.
One more thing rides on the card: which program should open it. A computer runs many programs at once. A port is a number saying which program a packet is for.
The address gets the card to the right building. The port gets it to the right room.
Room 80 is where a web server listens, and the card is addressed to 172.66.147.243. The other room numbers are examples.
An example run. The lesson says all of this can happen, not how often.
4 of 8 Try it
UDP, the postcard
Given an address that is easy to fake and a room number that says who is home, there are two honest ways to hold a conversation. The plainest: write the postcard and send it, no questions asked. That is UDP.
It adds a return address, a room number, how long the message is, and a check that the card was not damaged. Each is 2 bytes, so 8 bytes of bookkeeping, and it stops there.
A byte is one small unit of data. One letter of plain text is about one byte.
UDP never asks if anyone is listening first. It never confirms a card arrived. It never notices when two cards arrive in the wrong order, or when one never arrives at all. Post six cards and see.
Nothing posted yet.
Six posted, five arrived, two of them swapped, and not one word came back to you. UDP does exactly what the postcard does, and no more.
That is fine when a lost postcard costs you nothing but sending it again. Take a DNS question, asking the internet's address book for a site's address. It is 29 bytes.
Over UDP it is 8 + 29 = 37 bytes on the wire, and if it is lost you ask again. It is the wrong choice when losing a card, or trusting a forged one, actually matters.
What changed
- Bytes on the wire for one question: 29 → 37
Example numbers. Real starting numbers are random.
Example numbers. A real random number is much bigger.
5 of 8 The fix
TCP, the phone call
The second way is to check the line is open before you say anything that matters. You pick up a phone and wait to hear an actual voice before launching into your news.
That is TCP. The check it runs before every conversation is the handshake: three messages, back and forth, before either side sends anything real.
In plain words: hello, hello back, ok. Each message carries a number, which the next step explains. Press Play to make the call.
Press Play to make the call.
What changed
- Messages sent before the first real word: 0 → 3
The three messages have names, and you will see them in captures: hello is a SYN, hello back is a SYN-ACK, ok is an ACK.
Why does the real reply say 1,001?
The hello itself uses up one number. That way a lost hello is noticed and sent again, because nobody acknowledges a number that never arrived.
Why numbers at all? Each end opens by saying a starting number, and each must read the other's number back before either trusts the line. Only someone who truly heard you can read your number back.
Here a forger mails a hello with a trusted machine's return address. Try both kinds of starting number.
If the site always starts at 8,000, the forger writes 8,000 without hearing anything, and the check passes.
The reply goes to the address the forger wrote, so the forger never hears the number and has to guess. A start that is cheap to predict makes the whole check worthless. That is the weakness the Christmas Day case exposed.
TCP's ordered, reliable line does not exist on the wire. The routers in the middle remember nothing about your conversation, which is what keeps them cheap enough to carry millions at once.
The two ends build the line themselves, out of starting numbers, acknowledgements (messages saying "I got it") and timers.
The layer underneath, IP, is best effort: no promise to deliver, to keep order, or to say it failed.
29.6 − 17.7 = 11.9 ms, about one round trip (the round trip there was about 14 ms) · medians, the middle of five runs each, so a slow run would be worse
UDP: 8 + 29 = 37 bytes · TCP: 3 × 40 + 20 + 2 + 29 = 171 bytes · 171 ÷ 37 = 4.6 times the bytes
4 round trips × 13 ms = 52 ms, the worst case, before the first byte
1,000,000 bytes ÷ 1,388 bytes a packet ≈ 720, about 700 packets · 13 ms ÷ 700 ≈ 0.02 ms of handshake per packet
6 of 8 The cost
What the call costs
The line is not free. Nothing real can go until the hello has crossed and the hello back has returned. That wait is one round trip.
The lesson timed it on a real connection: the handshake took 12.9 ms, and the kernel's own estimate of the round trip was 13 ms.
ms is a millisecond, a thousandth of a second.
What changed
- Wait before the first real byte: 0 ms → 13 ms
Does one round trip matter? The lesson sent the same DNS question to the same server both ways, five runs each, and kept the median, the middle result.
Over UDP it took 17.7 ms. Over TCP it took 29.6 ms, and 14.3 ms of that was the handshake alone. TCP costs one extra round trip before the first byte, and that is a fixed cost, not a percentage.
The byte count says the same thing from the other side. For one small question, the machinery costs more than the message.
TCP's four packet close is not even counted here, and its header reaches 32 to 40 bytes with the usual options, so 20 is the minimum.
A real page load pays a round trip for several things: finding the address (DNS), the TCP handshake, TLS (a second hello that locks the line, covered in a later lesson), and the request itself. At this line's 13 ms each, pick a case.
Warm means the address is already remembered, TLS is resumed, and the open TCP call from before is reused, called keep-alive. Four round trips or one, for the same page.
So when is the handshake worth it? For one small question it costs more than the message, which is the real reason DNS runs on UDP: a lost question is cheaper to ask again than a connection is to set up.
Move a megabyte instead and the same one wait is shared by about 700 packets, and it disappears into the noise.
The price of skipping TCP is writing loss detection yourself, which is exactly what QUIC did.
7 of 8 Your guess
Your picture now
Goal met: a line you can trust, on a network that promises nothing. It cost 3 messages and one round trip, about 13 ms, before the first real word. Which picture is closest now?
Pick one.
c is TCP: two ends, three messages, and a count kept at both ends. a is UDP, real but with no promises. b is not how the internet works: the middle remembers no conversation, so no wire is set aside for yours.
8 of 8 Keep this
Keep this
A network promises nothing, and a packet's return address can be faked. UDP is a postcard: 8 bytes, no promises. TCP is a phone call: hello, hello back, ok, with starting numbers each end must read back.
The price: one round trip, about 13 ms on this line, before the first real word. A cold page load pays 4 of them, 52 ms.
Read the full lessonComing up The call is open. Now a page read down the line goes missing in the static. Next: how TCP numbers every page and hears back what arrived, so nothing is lost.