TCP Handshakes & Reliability
The network loses packets and tells nobody. TCP is how two machines get every byte across anyway.
An interactive Networking lesson: 23 steps, about 32 minutes, on a live simulation in your browser.
A checkout service on hostA has to get orders to a warehouse server on hostB, two routers away. The simplest way to send anything is UDP: put the bytes in one packet, write the destination address and port on it, and let go.
Watch order-1001 cross r1 and r2. hostB runs an echo service on UDP port 7, so the same bytes come straight back and hostA knows this one arrived.
What you will learn
Send and hope
- Send and hope
- One packet goes missing: The network promises nothing: a packet arrives or it silently does not, and the sender is not told which.
Three packets to agree
- The three-way handshake: SYN: my count starts at x. SYN-ACK: heard you, mine starts at y. ACK: heard you too. Each side has now sent a number and had it confirmed.
- What each packet proves: A handshake costs one round trip. The client may send as soon as the SYN-ACK lands; the server must wait for the third packet.
Counting bytes
- Sequence numbers count bytes: seq says where this segment's first byte sits in the stream. ack says which byte I want next, which means I hold everything before it.
- One ACK covers many segments: ACKs are cumulative. One ACK confirms every byte before it, so a lost ACK is repaired by the next one.
When a segment is lost
- A segment goes missing: A receiver cannot acknowledge past a hole. Duplicate ACKs are the hole announcing itself, and three of them trigger a retransmission.
- Break it: lose the last segment: TCP notices loss two ways: three duplicate ACKs (fast, but needs later data to arrive) or the retransmission timer (slow, but always works).
- Drill: capture the conversation
Refused or timed out
- A lost SYN: A connection that takes almost exactly 1 s, 3 s or 7 s to open lost one, two or three SYNs on the way.
- Nothing is listening: Connection refused is an answer. A machine received your SYN and its kernel replied: nothing listens on that port.
- Break it: a black hole: A timeout is the absence of an answer. Something on the path ate the packet, or nobody is at that address.
- Refused versus timeout
- Drill: test a port
How fast to send
- Slow start: probing the path: cwnd is the sender's guess at what the path can carry. It starts small and doubles every round trip until something says stop.
- Past the threshold: Below ssthresh the window doubles every round trip. Above it, the window grows by about one segment per round trip.
- What a loss does to the window: Loss is the network's only way to say 'too fast'. Add one segment per round trip while things go well; halve when a segment is lost.
- Break it: a timeout
Ending a connection
- Closing with FIN: FIN means 'I am done sending', not 'hang up'. Each side sends its own, and each is acknowledged.
- TIME_WAIT: The side that closes first pays with TIME_WAIT: it keeps the connection's addresses and ports reserved for a minute after the last packet.
- Break it: RST, the abrupt end: FIN ends a connection in order and loses nothing. RST throws it away on the spot: refused if it answers a SYN, reset by peer if it comes later.
Recap & playground
- Cheat sheet
- Playground