Networking handbook · Chapter 11

TCP

A program writing 600 bytes to a TCP connection can expect its peer to read those bytes in order while the connection succeeds. The peer may read them in several pieces. Sequence numbers, acknowledgments and windows make that byte-stream contract possible.

In this guide13 sections

Introduction

A program writing 600 bytes to a TCP connection can expect its peer to read those bytes in order while the connection succeeds. The peer may read them in several pieces. Sequence numbers, acknowledgments and windows make that byte-stream contract possible.

Identify the conversation

Each side has an IP address and a 16-bit port. A server can serve many connections on the same listening port because the client endpoints distinguish them. After the initial SYN and SYN-ACK exchange, the client’s acknowledgment completes the usual three-way handshake.

Each direction has its own 32-bit sequence space. SYN establishes an initial sequence number and occupies one position. The first data byte uses the next number. FIN also occupies one position. An acknowledgment with no data or SYN/FIN consumes none.

Read the segment header

The data offset counts the TCP header in four-byte words. A value of five means 20 bytes. Options and padding can extend it to 60 bytes. Read this field before locating the data, since two segments on the same connection can have different header lengths.

The acknowledgment number, when ACK is set, identifies the next byte expected in sequence. The checksum covers the header and data plus an IP pseudo-header. This detects accidental corruption. An application requiring peer authentication and protection against modification also needs a security protocol such as TLS.

Separate receiver space from path capacity

The advertised receive window represents how much more data the receiver is prepared to accept. TCP congestion control independently limits how much the sender puts into the network. Either limit can constrain progress. A loss can trigger retransmission and delay delivery of later bytes already received.

For a first estimate of the bytes needed in flight, multiply the target bit rate by RTT in seconds and divide by eight. At 20 Mbit/s and 40 ms the result is 100,000 bytes. Protocol overhead and application behavior affect actual throughput.

Diagram

  1. Client sends SYN · seq 1000
  2. Server sends SYN + ACK · ack 1001
  3. Client acknowledges server SYN
  4. Client data starts at 1001
Follow the client’s sequence space from its handshake to its first data byte. The server chooses its own initial sequence number.

Worked examples

Account for 600 bytes and a FIN

  1. Start with client SYN sequence 1000. The next available client sequence number is 1001.
  2. Send 600 data bytes covering 1001 through 1600 inclusive. After receiving them contiguously, the server can acknowledge 1601.
  3. A FIN sent at 1601 consumes one more position. Once accepted, it advances the acknowledgment to 1602. A retransmission of the original 600 bytes still uses 1001 through 1600.

TCP/UDP Port Reference: Read a DNS resolver configuration

  1. A resolver configuration lists DNS on port 53. Look up 53 and consider both UDP and TCP.
  2. For a conventional UDP query, model the client as sending from a temporary port to destination UDP 53. The response reverses those endpoints.
  3. A TCP exchange with the same DNS service uses TCP 53. Record the transport explicitly when describing the two cases.

Reference table

TCP reference
TCP fieldSizeReading it
Source / destination ports16 bits eachIdentify the endpoint at each side
Sequence / acknowledgment32 bits eachFirst sequence position and next expected position
Data offset / control bits4-bit offset plus reserved and control fieldsHeader length and signals such as SYN, ACK and FIN
Window16 bitsReceive credit, with negotiated scaling where applicable
Checksum / urgent pointer16 bits eachIntegrity check and urgent-data boundary when URG is set
Options and padding0–40 bytesMay carry MSS, window scaling or timestamps

Configuration example

  • For an application on TCP 8443, record both the listening address and port in the service definition. Configure the client with that same service endpoint.
  • Budget a connection timeout separately from an application response deadline. A successful handshake can precede a slow or failed application operation.

Common mistakes

  • Assuming one write at the sender always becomes one read at the receiver. A framed application protocol needs its own length or delimiter.
  • Treating a TCP acknowledgment as proof that a database committed a transaction. It confirms progress in the peer’s TCP receive sequence.

Frequently asked questions

Does an ACK always carry data?

No. An acknowledgment can appear on a data segment or on a segment with no payload.

What happens after the last 32-bit sequence number?

Sequence arithmetic wraps modulo 2³². TCP interprets values relative to the current windows so a connection can transfer far more than 4 GiB.

What does the TCP handshake establish?

The opening exchange synchronizes connection state and initial sequence numbers between endpoints. It does not prove that the application request will succeed.

Does TCP preserve application write boundaries?

No. TCP provides an ordered byte stream. Applications need their own framing, such as fixed-size records, delimiters, or a length prefix.

How does TCP respond to loss?

TCP uses acknowledgments, timers, and retransmission behavior to recover missing data. Congestion control also adjusts sending behavior when network capacity is constrained.

Is a TCP connection identified only by a port number?

No. A connection is distinguished by endpoint addresses and ports plus protocol context. The same port number can be used by many different connections.

How are flow control and congestion control different?

Flow control protects a receiver from being overrun using its advertised receive capacity. Congestion control responds to conditions in the network path to avoid overwhelming shared capacity.

Does TCP provide message boundaries?

No. It exposes an ordered byte stream. An application must frame its own messages, even when one write happens to align with one segment.

What is the difference between orderly close and reset?

An orderly FIN-based close signals that a sender has finished its stream direction. A reset aborts connection state and can indicate an error or explicit rejection.

Does TCP encryption come from TCP itself?

No. TCP transports bytes without encrypting them. Protocols such as TLS can protect application traffic carried over TCP.

What is a TCP MSS?

The Maximum Segment Size is the maximum TCP data payload an endpoint advertises for a segment, excluding the IP and TCP headers. It is commonly adjusted to fit the path MTU.

Why might a TCP handshake succeed but data stall?

The opening packets may fit while later larger packets encounter an MTU problem, loss, filtering, or application delay. Check retransmissions, acknowledgments, and path behavior.

What is a retransmission timeout?

It is a timer used to retransmit data that has not been acknowledged. TCP adapts timing based on observed round-trip behavior rather than assuming one fixed delay for every path.

What does the TCP receive window control?

It advertises how much data the receiver can accept before more acknowledgment is needed. Window scaling allows larger advertised windows on high-bandwidth or high-delay paths.

Do TCP ports identify applications by themselves?

No. A port is interpreted with protocol, endpoint address, socket state, and operating-system binding. Services can use non-default ports or share a host address.

Further learning and references