TCP: reliable byte streams
TCP provides reliable, ordered, connection-oriented delivery over the unreliable IP layer. It carries most internet traffic, including the web, email, and file transfer.
TCP's service
The Transmission Control Protocol presents applications with a reliable, ordered, full-duplex byte stream between two endpoints. Applications write bytes; TCP divides them into segments, numbers them, retransmits lost ones, discards duplicates, and delivers bytes in order. It also performs flow control and congestion control. TCP was specified in RFC 793 (1981), now updated by RFC 9293 (2022).
The TCP header
A TCP header, at least 20 bytes, includes source and destination ports, a 32-bit sequence number (the position of the segment's first byte in the stream), an acknowledgement number (the next byte expected), header length, flags such as SYN, ACK, FIN, and RST, a receive window size for flow control, a checksum, and options such as maximum segment size and window scaling.
The three-way handshake
A client opens a connection by sending a SYN segment with an initial sequence number x. The server replies with SYN-ACK, carrying its own initial sequence number y and acknowledging x + 1. The client sends an ACK for y + 1, and data can flow. Initial sequence numbers are chosen unpredictably to avoid confusion with old segments and to resist spoofing.
Cumulative acknowledgements
TCP acknowledgements are cumulative: ACK number n means 'I have received all bytes up to n - 1; send byte n next'. If a segment is lost, later segments arriving out of order trigger duplicate ACKs for the missing byte. Receivers often delay ACKs briefly to acknowledge two segments at once, reducing overhead.
Timeouts and fast retransmit
TCP retransmits a segment if its timer expires. The retransmission timeout is set from a smoothed RTT estimate plus a margin for variation, so it adapts to each path. Waiting for timeouts is slow, so fast retransmit resends a segment immediately after three duplicate ACKs, a strong hint that it was lost while later segments arrived.
Flow control
Flow control prevents a sender from overwhelming a slow receiver. The receiver advertises its free buffer space as the receive window (rwnd) in every segment; the sender keeps the amount of unacknowledged data below that window. If the window falls to zero, the sender pauses and periodically probes until space opens.
Closing connections
Either side ends its direction of data with a FIN segment, acknowledged by the other side; the connection closes fully when both directions have finished. The side that closes first enters a TIME_WAIT state for a while, typically up to a few minutes, to handle delayed segments. A RST segment aborts a connection immediately, for example when a port has no listening application.
Worked example
A client sends three 1,000-byte segments starting at sequence numbers 1, 1001, and 2001. The second is lost. The receiver ACKs 1001 after the first and, on receiving the third out of order, sends a duplicate ACK 1001. If the sender then sends segments at 3001, 4001, and 5001, each also triggers ACK 1001. After three duplicate ACKs, fast retransmit resends 1001; once it arrives, the receiver ACKs 6001, covering everything.
Key takeaways
TCP provides a reliable, ordered, full-duplex byte stream with flow and congestion control (RFC 793, updated by RFC 9293). Its header carries ports, sequence and acknowledgement numbers, flags, window size, and checksum. The three-way handshake exchanges SYN, SYN-ACK, and ACK with unpredictable initial sequence numbers. ACKs are cumulative; out-of-order arrivals cause duplicate ACKs. Adaptive timeouts and fast retransmit after three duplicate ACKs recover losses. Flow control uses the advertised receive window. FIN closes each direction, TIME_WAIT absorbs stray segments, and RST aborts.
Flashcard
Reliable, ordered byte-stream transport
Check your understanding
A TCP receiver has buffer space for only 8,000 more bytes. How does it stop the sender from overflowing it?
