TCP: handshakes, ordering and retries
IP delivers packets on a best-effort basis: some get lost, some arrive out of order. TCP still hands your program every byte once, in order. Here is how: a handshake to agree on numbers, a number on every byte, and a resend for whatever goes missing.
01 What the network does not promise
Your program writes bytes into a TCP socket, and the program at the other end reads the same bytes in the same order. Underneath, the bytes travel as IP packets, and IP promises very little. A packet can be:
- lost, dropped by a full router queue or a bad link;
- reordered, because two packets took different paths;
- duplicated, for example when a lower layer resends;
- delayed, sometimes by seconds.
TCP hides all of this with three ideas: a handshake that sets up shared state on both ends, a sequence number for every byte, and acknowledgements that report what arrived, so the sender can resend what did not. (TCP also does flow and congestion control, which this page leaves out.)
02 The three-way handshake
Before any data moves, each side picks a starting sequence number, its ISN (initial sequence number), and each must learn the other’s. Real ISNs are random 32-bit numbers, which makes them hard for an attacker to guess. Here the client picks 100 and the server 300 so the numbers stay readable.
- just sent
- just changed
- connection open
The server has called listen() and is waiting. The client has not sent anything yet.
Why three messages? Each side has to send its own ISN and hear it acknowledged, which is four messages. The server’s SYN and its ACK ride in one segment, so four fold into three. The price is one round trip before the first byte of data, which is why reusing connections (HTTP keep-alive, connection pools) matters.
03 A number for every byte
After the handshake, sequence numbers count bytes, not packets. A segment with seq=101 and 4 bytes of data carries bytes 101, 102, 103 and 104. The receiver replies ack=105: “I have everything up to 104; send 105 next.”
- first segment
- second segment
An ACK is cumulative: ack=109 covers every byte before 109, so losing an ACK does no harm if a later one gets through. The receiver also uses the numbers to put segments back in order and to throw away duplicates it has already seen.
04 When a segment goes missing
Now the client sends 300 bytes as three segments of 100, and the network drops the middle one.
- just arrived or sent
- lost on the way
- held, out of order
- given to the app
- not here yet
The client has 300 bytes to send, starting at byte 101, as three segments of 100. Nothing has gone out yet.
Waiting for the timer is slow. The timeout adapts to the measured round-trip time, with a floor of 200 ms on Linux (RFC 6298 suggests 1 second). So TCP has a faster path: when the sender gets three duplicate ACKs for the same number, it assumes that segment is lost and resends it at once. This is fast retransmit. In the trace only one segment followed the loss, so there was only one duplicate ACK and the timer had to fire.
05 Watch a handshake with tcpdump
Start a listener with nc, capture the loopback interface, and connect to it. The interface is lo on Linux and lo0 on macOS, and tcpdump needs root.
The first three lines of the capture are the handshake. Trimmed, they look like this; your ports and sequence numbers will differ, since ISNs are random.
[S] is a SYN, [S.] is a SYN plus an ACK (the dot means ACK), and [.] is a plain ACK. The server’s ack is the client’s seq plus one. After the handshake, tcpdump prints numbers relative to the ISN, which is why the third line says ack 1. Type into the client and you will see [P.] segments whose length matches what you typed. ss -tan on Linux, or netstat -an on macOS, lists every connection with its state, such as ESTAB or TIME-WAIT.
06 When it bites
connect()A round trip before the first byteEvery new connection pays one round trip, and TLS adds more on top. Reuse connections with keep-alive or a pool.seq=201 lostOne loss stalls everything behind itThe app can’t read bytes 301–400 until 201–300 arrive. This is head-of-line blocking, and it is one reason HTTP/3 runs over QUIC, which orders each stream separately.net.ipv4.tcp_syn_retries = 6Connecting to a dead host hangsOn Linux an unanswered SYN is retried 6 times by default, with the wait doubling each time, so connect() can take about two minutes to fail. Set your own connect timeout.TIME-WAITSockets that linger after closeThe side that closes first keeps the connection in TIME-WAIT (60 seconds on Linux) to catch late segments. Many short connections from one client can use up its local ports.SYN, SYN, SYN...Half-open connectionsA SYN flood sends SYNs and never finishes the handshake, filling the server’s queue. SYN cookies let the server reply without keeping any state until the final ACK.An ACK means the bytes reached the other machine’s kernel, not that the program read them or acted on them. If you need to know a request was processed, your protocol needs its own reply.