Course outline · 0% complete

0/25 lessons0%

Course overview →

TCP and the three-way handshake

lesson 3-2 · ~11 min · 7/25

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:

  1. Client → server: SYN ("I want to connect, my numbering starts at 100")
  2. Server → client: SYN-ACK ("OK, I heard 100. Mine starts at 300")
  3. 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.

ClientServer1. SYN (seq=100)2. SYN-ACK (seq=300, ack=101)3. ACK (ack=301)connection established, data can flow
The three-way handshake: SYN, SYN-ACK, ACK. Time flows downward on each vertical line, and each gold dot is one message crossing the network.

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.