Computer Networking › Section C · The transport layer › Topic C.5

Connection-oriented transport: TCP

TCP carries the web, email, file transfer and much more. It turns IP's unreliable datagrams into a reliable, ordered byte stream, using the principles of the previous topic plus a few clever refinements: byte-numbered sequence numbers, adaptive timeouts, fast retransmit and flow control.

The TCP connection

TCP, specified in RFC 793 in 1981 and now in RFC 9293 (2022), is connection-oriented: before data flows, the two ends exchange setup segments and create connection state, including buffers, sequence numbers and window sizes. That state lives only in the two end systems. A connection is point to point and full duplex, carrying data both ways at once. An application writes bytes into TCP's send buffer, and TCP takes chunks of up to one maximum segment size (MSS) of data per segment. On Ethernet, with its 1,500-byte frame payload, the MSS is typically 1,460 bytes: 1,500 minus 20 bytes of IP header and 20 of TCP header.

The segment header

A TCP header is at least 20 bytes and at most 60. It carries 16-bit source and destination ports; a 32-bit sequence number and a 32-bit acknowledgement number; a header-length field, needed because options vary; one-bit flags; a 16-bit receive window for flow control; a checksum, computed like UDP's; and a rarely used urgent pointer. The flags include SYN and FIN to open and close, ACK to mark the acknowledgement field valid, RST to abort, PSH, URG, and ECE and CWR for congestion signalling. Options include the MSS, window scaling, timestamps and selective acknowledgements (SACK).

Sequence and acknowledgement numbers

TCP numbers bytes, not segments. A segment's sequence number is the stream position of its first data byte. Each side picks a random initial sequence number (ISN), which avoids confusion with old segments and makes forged segments harder to inject. The acknowledgement number is the next byte the sender of the ACK expects. TCP ACKs are cumulative: ACK 6001 means every byte up to 6000 has arrived. Out-of-order data is usually buffered rather than discarded, although the standard leaves that choice to implementations. An ACK often rides on a data segment going the other way, which is called piggybacking.

Worked example: opening a connection

A client with ISN 8000 connects to a server whose ISN is 3000, then sends 1,000 bytes. 1. SYN: the client sends a segment with the SYN flag, sequence number 8000 and no data. 2. SYN-ACK: the server allocates state and replies with SYN and ACK set, sequence number 3000 and acknowledgement number 8001, because a SYN uses up one sequence number. 3. ACK: the client replies with sequence number 8001 and acknowledgement 3001; this third segment may already carry data. 4. Data: the client's 1,000 bytes occupy bytes 8001 to 9000, so the server acknowledges with ACK 9001. Check: the handshake costs one RTT before the client can send its first data.

Estimating the round-trip time

A retransmission timer must be longer than the RTT, but RTTs vary between paths and from moment to moment. TCP measures a SampleRTT for one segment at a time and smooths the samples with an exponentially weighted moving average (EWMA). Following RFC 6298, with \(\beta = 0.25\) and \(\alpha = 0.125\), it first updates the deviation, using the old estimate, then the estimate:

\mathrm{DevRTT} = 0.75\,\mathrm{DevRTT} + 0.25\,|\mathrm{Sample} - \mathrm{Est}|
\mathrm{Est} = 0.875\,\mathrm{Est} + 0.125\,\mathrm{Sample}
\mathrm{Timeout} = \mathrm{Est} + 4\,\mathrm{DevRTT}

Est stands for EstimatedRTT and Sample for SampleRTT. Without the timestamps option, samples are not taken from retransmitted segments, whose ACKs are ambiguous. Before any sample exists, the timeout starts at 1 second.

Worked example: updating the timeout

A connection has EstimatedRTT = 100 ms and DevRTT = 20 ms. Two samples arrive: 120 ms, then 80 ms. 1. First sample, deviation: 0.75 × 20 + 0.25 × |120 − 100| = 20 ms. 2. Estimate: 0.875 × 100 + 0.125 × 120 = 102.5 ms. 3. Timeout: 102.5 + 4 × 20 = 182.5 ms. 4. Second sample, deviation: 0.75 × 20 + 0.25 × |80 − 102.5| = 20.625 ms. 5. Estimate: 0.875 × 102.5 + 0.125 × 80 = 99.6875 ms, about 99.7 ms. 6. Timeout: 99.6875 + 4 × 20.625 ≈ 182.2 ms. Check: each sample moves the estimate only an eighth of the way towards itself. Real systems also impose a minimum timeout: RFC 6298 recommends 1 second, and Linux uses 200 ms.

Retransmission and fast retransmit

TCP's reliable transfer mixes Go-Back-N and selective repeat ideas. The sender keeps one retransmission timer, for its oldest unacknowledged segment. On a timeout it resends just that segment and doubles the timeout, an exponential backoff that eases pressure on a congested path. Receivers often delay an ACK briefly, typically to cover two segments at once. Timeouts are slow, so TCP also uses duplicate ACKs. When a segment goes missing, each later segment makes the receiver repeat the ACK for the gap. Three duplicate ACKs strongly suggest a loss, so the sender resends the missing segment at once, without waiting for the timer: fast retransmit.

Worked example: fast retransmit

A sender transmits five 500-byte segments starting at bytes 8001, 8501, 9001, 9501 and 10001. The segment at 8501 is lost. 1. The first segment arrives, and the receiver sends ACK 8501. 2. The segment at 9001 arrives out of order; the receiver buffers it and sends ACK 8501 again, the first duplicate. 3. The segments at 9501 and 10001 produce the second and third duplicates. 4. On the third duplicate ACK, the sender resends the segment at 8501. 5. It fills the gap, so the receiver acknowledges everything with ACK 10501. Check: the last segment covers bytes 10001 to 10500, and the next byte expected is 10501.

Flow control

Flow control stops a fast sender from overflowing the receiver's buffer. The receiver advertises its free space, the receive window, rwnd, in every segment: rwnd = buffer size minus (last byte received minus last byte read by the application). The sender keeps its unacknowledged data, last byte sent minus last byte acknowledged, at or below rwnd. For example, a 20,000-byte buffer that has received 14,000 bytes, of which the application has read 6,000, holds 8,000 bytes, so rwnd = 12,000. If rwnd reaches 0, the sender keeps sending tiny probe segments so it learns when space opens up.

Closing and resetting connections

Either side ends its direction of data by sending a FIN, which the other side acknowledges; the connection is fully closed when both directions are done. The side that closed first then waits in the TIME_WAIT state, often between 30 seconds and a few minutes, so that a lost final ACK can be resent and stray old segments die out. A segment with RST aborts a connection at once; a host also replies with RST to a SYN for a port where nothing is listening. Because a server allocates state on each SYN, a flood of forged SYNs can exhaust it; SYN cookies defend against this by storing nothing until the final ACK arrives.

Common mistakes to avoid

First, numbering segments instead of bytes. Second, reading ACK n as "byte n received"; it means n is the next byte expected. Third, forgetting that SYN and FIN each consume one sequence number. Fourth, computing DevRTT with the new estimate when following RFC 6298, which updates the deviation first. Fifth, confusing flow control, which protects the receiver, with congestion control, which protects the network. Sixth, thinking fast retransmit needs a timeout; three duplicate ACKs trigger it.

Key takeaways

TCP (RFC 9293) is a connection-oriented, point-to-point, full-duplex byte stream with state only at the ends. Its header of 20 to 60 bytes carries ports, 32-bit sequence and ACK numbers, flags, the receive window and a checksum. Sequence numbers count bytes from a random ISN, and ACKs are cumulative. The SYN, SYN-ACK, ACK handshake costs one RTT. Timeout = EstimatedRTT + 4 DevRTT, with weights 0.125 and 0.25, doubling after each expiry. Three duplicate ACKs trigger fast retransmit. rwnd provides flow control. FIN closes each direction; RST aborts.

Flashcards

Flashcard

TCP

Connection-oriented, reliable, full-duplex byte stream

RFC 793 (1981), now RFC 9293 (2022).

Test your understanding

Question 1 of 5

A segment carries bytes 4001 to 5460 and arrives in order. What acknowledgement number does the receiver send?

Question 2 of 5

EstimatedRTT is 200 ms and a new SampleRTT of 260 ms arrives. With α = 0.125, what is the new EstimatedRTT?

Question 3 of 5

A client sends SYN with sequence number 7000; the server's ISN is 2000. What does the SYN-ACK carry?

Question 4 of 5

A receiver's buffer holds 32,000 bytes. It has received bytes up to 24,000, and the application has read 10,000. What rwnd does it advertise?

Question 5 of 5

A TCP sender receives the same ACK number three extra times. What does it do?

← Previous topicNext topic →