Reliability on top of chaos
Lesson 3-1 left us with packets that can arrive late, out of order, or not at all. TCP (Transmission Control Protocol) fixes this with three habits:
- Sequence numbers on every byte, so the receiver can put pieces back in order (you did this by hand with
sort -n). - Acknowledgments (ACKs): the receiver constantly reports "I have everything up to byte N, send from there."
- Retransmission: if the sender does not hear an ACK in time, it sends the piece again.
Before any data flows, both sides must agree to talk and pick starting sequence numbers. That is the famous three-way handshake:
- Client → server: SYN ("I want to connect, my numbering starts at 100")
- Server → client: SYN-ACK ("OK, I heard 100. Mine starts at 300")
- Client → server: ACK ("I heard 300, we're on")
Three messages, one round trip and a half, and the connection exists. Every HTTP request in this course rides on a connection opened exactly this way.
Code exercise · bash
Run this. It narrates the handshake with real-looking numbers. Note how each ack is the other side's seq plus 1: an ack number always means "this is the next byte I expect".
Quiz
During a handshake, the client's SYN carries seq=500. What ack number does the server put in its SYN-ACK?
Code exercise · bash
Your turn. You are the receiver on an open connection. Each incoming segment below is seq:len (starting byte and length). For each one, compute the ack to send back: the next byte you expect, which is seq + len. Print the line shown in the expected output for each segment.