Computer Networks

TCP

Reliable, ordered, congestion-controlled byte streams between two ports.

Level: beginner

What is it?

The Transmission Control Protocol turns best-effort IP packets into a stream that, under ordinary loss, delivers every byte once, in order. It does this with sequence numbers, acknowledgements, retransmission, flow control (window), and congestion control.

Why does it matter?

Files, HTTP, SSH, email — anything that would be wrong if a byte went missing — sits on TCP. Knowing what TCP guarantees (and that it does not guarantee timely delivery) is the difference between a network bug and an application bug.

Mental model

A numbered conveyor belt with receipts. If a box is missing, you ask again. If the receiver's loading dock is full, you slow down. If the highway is jammed, you also slow down — that second brake is congestion control.

How it works

  • Connection-oriented: handshake, data, teardown.
  • Reliable and ordered: retransmit, reorder buffer, cumulative ACKs, SACK.
  • Flow control: advertised window. Congestion control: cwnd, ssthresh, slow start, AIMD.
  • Ports multiplex a host. A 4-tuple (src IP, dst IP, src port, dst port) is a connection.
  • TCP does not keep message boundaries. Two write()s can arrive as one read(), and vice versa.

Real-world usage

  • HTTP/1.1 and HTTP/2
  • SSH, SMTP, most databases' wire protocols

Common mistakes

  • Assuming a send() corresponds to one recv(). It is a stream.
  • Confusing flow control (protect the receiver) with congestion control (protect the network).
  • Believing TCP 'guarantees delivery'. It guarantees that if the connection stays up, bytes arrive. A reset or timeout is a failure.

Interview questions

TCP vs UDP in two contrasts.

TCP: connection, reliability, order, congestion control. UDP: datagrams, no connection, no retransmission, app-owned reliability.

What is the 4-tuple?

Source IP, destination IP, source port, destination port. NAT rewrites parts of it; that's why NAT is hard.

Prerequisites

Related concepts

Continue learning

Browse the full computer science library