Networking and Protocols

TCP

Reliable, connection oriented transport that delivers a byte stream in order.

Where you will see it: Shows up in packet captures, network diagrams, firewall rules, and system or network logs.

What it is

TCP (Transmission Control Protocol) is a transport protocol that provides reliable, ordered delivery between two endpoints. Applications write bytes to TCP and read bytes from TCP; TCP decides how to split that stream into segments, how to recover from loss, and how to present the data to the receiver in the correct order.

Key points
  • Starts with a handshake that synchronizes state between two endpoints.
  • Tracks sequence numbers so the receiver can reorder data and detect loss.
  • Uses flow control and congestion control so it does not overwhelm networks or receivers.

How it works in broad strokes

  1. The client and server perform a three way handshake (SYN, SYN ACK, ACK) to agree on initial sequence numbers and create per connection state.
  2. The sender streams data; TCP chops it into segments, numbers the bytes, and puts them on the wire.
  3. The receiver acknowledges what it has received, usually by saying the next byte it expects.
  4. If acknowledgements do not arrive in time, the sender retransmits the missing data. The receiver reorders segments and removes duplicates.
  5. Flow control limits how much data can be in flight based on the receiver window so a fast sender does not overwhelm a slow receiver.
  6. Congestion control adapts the sending rate to the network by probing capacity and backing off when it detects congestion signals such as loss or ECN marks.
  7. When finished, the connection is closed with FIN or aborted with RST, and TCP keeps short lived state (like TIME WAIT) to avoid mixing old packets with new connections.

Concrete example

You load a website. Your browser opens a TCP connection to the server, completes the handshake, then sends an HTTP request. If a packet is lost on the way, TCP retransmits it, and your browser still receives the full response in order without knowing a loss happened.

Why it matters

The Internet's base layer, IP, can drop, duplicate, or reorder packets. TCP exists to hide that reality from applications that need correctness, such as web browsing, APIs, email, SSH, and most database traffic.

Security angle

  • TCP is vulnerable to resource exhaustion such as SYN floods, so servers often use mitigations like SYN cookies and rate limiting.
  • Sequence number prediction and reset injection are classic risks, but modern stacks add hardening. Use TLS to protect application data and endpoints.
  • Middleboxes like NATs and firewalls track TCP state. Misconfigured state timeouts can break long lived connections.

Common pitfalls

  • Assuming one application write equals one packet. TCP is a stream, not a message protocol.
  • Forgetting that performance depends on round trip time and loss. Small loss can crush throughput.
  • Ignoring MTU issues. Path MTU problems can look like random timeouts or stalled transfers.
  • Misreading TIME WAIT as a bug. It is normal and protects new connections.
  • Treating port 443 as automatically safe. TCP only provides transport, not trust.

DEEP DIVE

Connection setup is negotiation plus state

A TCP connection is not just a pipe. Both endpoints create state keyed by the 4 tuple: source IP, source port, destination IP, destination port. When a client calls connect, it sends a SYN segment that proposes an initial sequence number and often a set of options such as MSS, window scaling, and SACK permission. The server replies with SYN ACK, choosing its own initial sequence number and confirming what it accepts, and the client finishes with an ACK. After that point both sides agree on how to number bytes and how to interpret acknowledgements.

That handshake also prevents a big class of confusion. Without it, an old delayed packet from a previous conversation could look like valid data for a new conversation. The handshake forces both sides to prove they are live now and to synchronize sequence number spaces before application data is delivered.

If you look at a packet capture, you will often see more than the classic three segments. Options are how TCP adapts to real networks. MSS prevents oversized segments. Window scaling allows large receive windows on high bandwidth paths. SACK lets a receiver tell the sender which blocks arrived out of order, which reduces needless retransmissions on lossy links.

Reliability is careful bookkeeping, not magic

TCP delivers a byte stream, which means the application does not see message boundaries. Internally, every byte has a position. The sender groups bytes into segments and labels them with sequence numbers. The receiver acknowledges what it has received using acknowledgements that are usually cumulative, meaning it says the next byte it expects. If a segment is lost, the receiver either stays silent about that gap or acknowledges past it only when it can, and the sender eventually retransmits.

Loss detection is a mix of timers and hints. A retransmission timeout is the last resort. Faster recovery comes from duplicate ACKs: if the receiver keeps acknowledging the same next byte, the sender can infer a missing segment and retransmit early using fast retransmit. With SACK, the receiver can be precise about which data it already has, so the sender can focus retransmissions on what is truly missing.

Ordering is handled by buffering. If segments arrive out of order, the receiver can store them and still acknowledge what it has, but it will not deliver bytes to the application until the missing gap is filled. This is why a single lost packet can stall an otherwise fast flow, sometimes called head of line blocking.

Flow control versus congestion control

Flow control protects the receiver. Each ACK carries an advertised receive window that tells the sender how much additional data the receiver can buffer. The sender must respect that limit even if the network could handle more. If the window drops to zero, the sender pauses and probes periodically until the receiver opens the window again.

Congestion control protects the network. TCP maintains a congestion window that limits how much data can be in flight without being acknowledged. At any moment, the sender is effectively constrained by the smaller of the congestion window and the receive window. This separation is important because receiver overload and network congestion are different problems with different signals.

Real congestion control algorithms vary by operating system and era, but the core idea is stable. Start cautiously, increase the sending rate while acknowledgements come back smoothly, and cut back when loss or delay suggests congestion. That is why throughput ramps up, then stabilizes, and sometimes drops sharply when the path becomes busy.

Typical lifecycle in real usage

Most modern applications keep connections open and reuse them. A browser, for example, may open a small number of TCP connections to a site and send many HTTP requests over each. Reuse matters because connection setup costs at least one round trip, and slow start means the sender cannot immediately blast at full speed.

When a connection is idle, middleboxes can forget state. NAT devices and firewalls often time out mappings, so applications that need long lived connections use keepalives or periodic traffic. On lossy wireless links, you will see retransmissions and variable latency, and TCP will adapt by reducing its congestion window and then rebuilding it.

When the application is done, the clean shutdown is a FIN exchange. Each direction can close independently, which is why you may see one side send FIN and still receive data from the other side for a while. After closing, one side enters TIME WAIT to make sure late packets from the old connection do not corrupt a future connection that happens to reuse the same 4 tuple.

What to look for when something feels slow

If ordering stalls, look for loss. A capture may show repeated duplicate ACKs, SACK blocks that point at a gap, and retransmissions. Even small loss rates can crush throughput on long delay paths because each loss event triggers backoff.

If throughput is capped, check the limiting window. A tiny receive window suggests the receiver or application is not reading quickly. A tiny congestion window suggests the sender is being cautious due to congestion signals. Measuring round trip time and retransmission rates helps you decide which story fits.

If the connection repeatedly resets, you might be seeing a firewall policy, a crash, or an application that is timing out and aborting. An RST is a hard abort, while a FIN is a graceful close. That difference matters for debugging.