The idea: number every byte, ACK the next one you need
IP may lose, delay or reorder packets. TCP still gives the app every byte, once, in order. It does that with three tools. Every byte has a sequence number. The receiver answers with an ACK number: the next byte it needs. And the sender keeps a copy of every byte until it is ACKed, and sends it again when it seems lost: after three duplicate ACKs, or when the retransmission timer fires.
The canvas is a time-down diagram, like the one on the TCP handshake page, which shows how this connection was set up. Time runs down, in ms from the start of each action. The client sends data from the left, the server ACKs from the right. A long quiet stretch is drawn short and marked ≈. The panel on the right shows the sender's variables (SND.UNA, SND.NXT, the timer), the RTT estimator and the receiver's buffer.
Sequence and ACK numbers
During the handshake each side picks an initial sequence number (ISN). Here the client's ISN is 1000, so its first data byte is 1001. The app writes 600 bytes. TCP cuts them into segments of at most MSS = 100 bytes. Each segment carries seq, the number of its first byte: 1001, 1101, … 1501. When bytes 1001–1100 arrive, the receiver sends ACK=1101. It means "I have everything before 1101; send 1101 next". The sender keeps two pointers: SND.UNA, the oldest byte not yet ACKed, and SND.NXT, the next byte it will send. Bytes between them are in flight. The receiver keeps RCV.NXT, the next byte it expects (Demo 1: no loss).
Cumulative ACKs
An ACK covers every byte before its number, not just the last segment. So when an ACK is lost, the next one repairs it. In Demo 2 the network drops ACK=1201. Ten ms later ACK=1301 arrives and moves SND.UNA from 1101 to 1301 in one go. Nothing is resent, and the transfer ends at 150 ms, as with no loss. Compare the per-packet ACKs of Selective Repeat on the ARQ protocols page, where a lost ACK does cost a resend.
A lost segment and duplicate ACKs
In Demo 3 the second segment, 1101, is lost. Segments 1201, 1301, 1401 and 1501 still arrive. The receiver cannot give them to the app yet, because bytes 1101–1200 are missing, so it holds them. Each time, it sends ACK=1101 again. To the sender these are duplicate ACKs. One or two duplicates may just mean the network reordered packets. Three mean that three later segments got through and 1101 really is gone. So on the third duplicate the sender resends 1101 at once: fast retransmit (RFC 5681).
The resent 1101 reaches the receiver at 190 ms and fills the gap. The receiver now delivers 1101 and the four held segments together, and its ACK jumps from 1101 to ACK=1601. Without fast retransmit the sender would have waited for its timer, which was set to fire at 350 ms.
The retransmission timer: SRTT, RTTVAR and RTO
Duplicate ACKs need later segments. When the last segment is lost (Demo 4), nothing comes after it, so no duplicates arrive. Only the timer can help. How long should it wait? Too short, and it resends segments that were only slow. Too long, and the connection sits idle. So TCP measures the round-trip time and keeps two running averages (RFC 6298). Each new RTT sample R updates them:
RTTVAR = 3/4 · RTTVAR + 1/4 · |SRTT − R| (how much the RTT varies) SRTT = 7/8 · SRTT + 1/8 · R (the smoothed RTT) RTO = max(RTO_min, SRTT + 4 · RTTVAR)
The first sample sets SRTT = R and RTTVAR = R / 2. Here the handshake measured 100 ms, so the page starts with SRTT 100, RTTVAR 50 and RTO 300 ms. RTO_min is 200 ms on Linux; RFC 6298 asks for 1 s. The timer runs while data is in flight. Each ACK of new data restarts it. When everything is ACKed it stops.
| Demo | What happens | Numbers |
|---|---|---|
| 1: no loss | six samples of 100 ms; RTTVAR shrinks | RTO 300 → 250 → 212.5 → 200 (the floor); RTTVAR 50 → 8.9 |
| 4: last segment lost | the timer, restarted by ACK 1501 at 140 ms, fires at 340 ms | resend 1501; RTO doubles 200 → 400; done at 440 ms |
| 5: RTT grows to 160 ms | SRTT climbs 1/8 of the way each sample; RTTVAR jumps | SRTT 100 → 133.1, RTTVAR 8.9 → 34.1, RTO 200 → 269.4 |
Two more rules. After a timeout RTO doubles (exponential back-off), because the network may be in trouble. And an ACK for a segment that was sent twice gives no sample: the sender cannot tell which copy it answers. This is Karn's rule. You see it in Demo 3: ACK=1601 covers the resent 1101, so RTO stays 250 ms.
The receive window in every ACK
Each ACK also carries win, the free space in the receiver's buffer. The sender never has more than win bytes in flight: it may send up to byte SND.UNA + win − 1. In Demo 6 the window is 300 bytes. The sender sends three segments, then stops until ACK=1101 slides the window forward at 100 ms. The transfer takes 220 ms instead of 150. A small window also hurts loss recovery: with only three segments in flight, at most two duplicate ACKs can come back, so the sender has to wait for the timer. What happens when the reader is slow and the window shrinks to zero is on the TCP flow control page. The sender's own limit, the congestion window, is on the TCP congestion control page.
Closing: FIN, CLOSE_WAIT and TIME_WAIT
Each direction closes on its own. close() on the client sends a FIN. A FIN takes one sequence number, so after the data 1001–1600 the FIN has seq 1601 and its ACK is 1602. The server ACKs it and goes to CLOSE_WAIT. Its app reads end-of-file and calls close() as well, which sends the server's FIN (seq 5001) and moves it to LAST_ACK. The client ACKs that FIN with 5002 and enters TIME_WAIT (Demo 7).
TIME_WAIT lasts 2·MSL (twice the maximum segment lifetime; 60 s on Linux). It has two jobs. If the last ACK is lost, the server resends its FIN, and the client is still there to ACK it (Demo 8: the server resends its FIN at 370 ms, and the client ACKs it again; the diagram counts time from the call to close()). And no old, delayed segment of this connection can end up in a new connection that reuses the same ports. A busy client that opens many short connections can collect thousands of sockets in TIME_WAIT. The HTTP connection page shows that in context.
Seeing it on Linux
ss -ti prints these numbers for each connection, for example rto:204 rtt:1.25/0.5 (SRTT / RTTVAR in ms) and retrans:0/3. nstat -az TcpRetransSegs TcpExtTCPFastRetrans TcpExtTCPTimeouts counts resent segments, fast retransmits and timeouts for the whole machine. ss -tan state time-wait lists the sockets in TIME_WAIT.
See also TCP vs UDP for what a protocol without any of this looks like.
What the page leaves out
Real receivers usually delay ACKs (one ACK per two segments, or after up to 40 ms). Most connections also use SACK, which tells the sender exactly which blocks arrived, and Linux finds losses with RACK, a time-based method, rather than only by counting duplicates. The congestion window and fast recovery after a loss (see TCP congestion control) are not modelled here. Also left out: the timestamps option that gives an RTT sample on every ACK and protects against wrapped sequence numbers (PAWS), window scaling, Nagle's algorithm, the FIN_WAIT_2 timeout (tcp_fin_timeout), simultaneous close and RST.