1. Home
  2. Explore
  3. Computer Science
  4. TCP Concepts Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

TCP Concepts Overview

TCP Deep Dive: Understanding the Transport Layer

The Transmission Control Protocol (TCP) is a core protocol of the internet, providing reliable, connection-oriented data delivery. This guide covers TCP fundamentals in depth, using real Wireshark captures to illustrate how the protocol works in practice. Whether troubleshooting network issues, analyzing security, or simply preparing for a CCNA, a solid grasp of TCP is essential.


The Three-Way Handshake: SYN, SYN-ACK, ACK

The TCP connection begins with a three-way handshake that synchronizes sequence numbers between the two hosts. The handshake consists of three packets:

  1. SYN – The client sends a SYN segment with its own initial sequence number (ISN).

  2. SYN-ACK – The server responds with a SYN that contains its own ISN, and an ACK that acknowledges the client’s ISN by incrementing it by one.

  3. ACK – The client acknowledges the server’s ISN, again by adding one. At this point both sides are synchronized.

Why “SYN”?

The term SYN comes from synchronize. The handshake synchronizes the sequence numbers of both directions. The SYN flag is set only in the first two packets of the handshake. After the handshake, the ACK flag remains set for all subsequent TCP segments (except in rare cases like a reset without ACK).

Initial Sequence Number (ISN)

Each side selects a randomly generated 32-bit initial sequence number. This number is the starting point for counting all bytes sent in that direction. Because the number is large and random, it helps prevent confusion with old connections. Wireshark often displays relative sequence numbers (starting at zero) to make the numbers human-friendly.

The ISN is a random high number. Wireshark subtracts the initial value to show relative numbers, making analysis much easier.

The “Ghost Byte”

When a host sends a SYN, it consumes one unit of sequence number space, even though no actual payload data is sent. This is called a ghost byte. The receiver must acknowledge this by incrementing its acknowledgment number by one. Similarly, the server’s SYN also consumes one unit. Therefore, after the three-way handshake, both sequence numbers are at relative value 1, and acknowledgment numbers are at 1.

Step-by-Step Wireshark Analysis

  • Packet 1 (SYN from client): Source port is a high ephemeral number (e.g., 54846), destination is port 80. The SYN flag is set; ACK flag is not set. The raw sequence number is large (e.g., 7413). Wireshark shows a relative sequence number of 0. The next expected sequence number after this SYN is 1 (relative). The acknowledgment number is zero because no ACK is sent.

  • Packet 2 (SYN-ACK from server): The server sets SYN and ACK flags. The acknowledgment number is the client’s sequence number + 1 (relative 1). The server also sends its own ISN (e.g., 5106 raw, relative 0). The TTL and other IP fields indicate the server’s starting TTL (e.g., 255) and approximate distance (e.g., 17 hops).

  • Packet 3 (ACK from client): The client sends an ACK with its sequence number = 1 (relative) and acknowledgment number = server’s sequence number + 1 (relative 1). This completes the handshake. From this point, the TCP connection is established.


Sequence Numbers and Acknowledgment Numbers

Sequence numbers track the byte stream in each direction. Acknowledgment numbers tell the sender what the receiver has successfully received. The relationship is:

  • Each byte of data increments the sequence number by one.

  • The acknowledgment number is the next expected sequence number (i.e., the last received sequence number plus the number of bytes received).

How They Work Together

When the client sends 501 bytes of data (e.g., an HTTP GET) starting at relative sequence number 1, the next expected sequence number becomes 502. The server responds with an acknowledgment number of 502. This tells the client that all 501 bytes were received correctly. The client can then free that data from its send buffer.

Sequence numbers are entirely direction-dependent. The client’s sequence number never mixes with the server’s; they are two independent counters.

Relative vs. Raw Sequence Numbers

Wireshark defaults to showing relative sequence numbers (starting at zero for each direction). This simplifies analysis. The raw numbers are still available in the packet details. To switch between views, use: Edit → Preferences → Protocols → TCP → Relative sequence numbers.

Practical Troubleshooting with Sequence Numbers

  • If a retransmission occurs, the sender will see the same sequence number (and data) resent because the acknowledgment number did not increase.

  • For multi-point captures (e.g., client side and server side), the raw sequence number can survive NAT or PAT. Filtering by raw sequence number helps correlate the same packet across capture points.

  • When troubleshooting, manually writing “Client” and “Server” columns on paper and tracing sequence numbers is a common old-school technique.


TCP Options: Exchanged Only in the Handshake

The TCP header can include optional features that are negotiated only during the three-way handshake. If the handshake is not captured, these options are unknown, which can mislead analysis. The header length is a 4-bit field (maximum 60 bytes for the TCP header). Options fill this space.

Key Options

Maximum Segment Size (MSS)

  • MSS is an option in the SYN and SYN-ACK that advertises the largest payload (data) a host can receive in a single TCP segment.

  • It is not negotiated; each side simply announces its own limit.

  • Common values: 1460 bytes (from client) and 1440 bytes (from server) – these depend on the path MTU.

Window Scale

  • The original TCP window field is only 16 bits, limiting the receive window to 65535 bytes. As networks became faster, this became a bottleneck.

  • Window scale is an option that multiplies the advertised window by a shift factor (2^shift count).

  • The shift count is exchanged in the SYN and SYN-ACK. If both sides support it, window scaling is used for the connection.

  • Example: A shift count of 6 means multiply the window by 26=642^6 = 6426=64. The true receive window becomes 65535×64≈465535 \times 64 \approx 465535×64≈4 MB.

Selective Acknowledgment (SACK)

  • SACK allows the receiver to inform the sender exactly which segments arrived (if some are missing), instead of only acknowledging the highest contiguous byte.

  • This improves retransmission efficiency. SACK is negotiated in the handshake.

Other Options

  • Timestamp – Used for calculating round trip time and protecting against wrapped sequence numbers.

  • Nop – Padding to align options.

Without capturing the handshake, Wireshark cannot calculate the true window size because the window scale multiplier is unknown. Always capture the handshake for full TCP analysis.


TCP Flags

The flags field (9 bits) indicates the purpose of the TCP segment. The most commonly used flags are:

Flag

Name

Purpose

SYN

Synchronize

Initiate a connection.

ACK

Acknowledgment

Indicates the acknowledgment number is valid. Set in all packets after the first SYN.

FIN

Finish

Gracefully close a connection.

RST

Reset

Abruptly terminate a connection (e.g., due to error or rejection).

PSH

Push

Instructs the receiving TCP stack to deliver data immediately to the application.

URG

Urgent

Indicates urgent data (rarely used today).

ECE

ECN Echo

Explicit Congestion Notification – used for congestion signaling.

CWR

Congestion Window Reduced

Part of ECN.

NS

Nonce Sum

Very rarely seen in practice.

Filtering on Flags in Wireshark

To filter on a specific flag, use the syntax tcp.flags.<flag> == 1. For example, tcp.flags.syn == 1 shows all packets with the SYN bit set. Wireshark also provides a right-click menu to copy the filter expression directly.


Receive Window and Window Scaling

The TCP receive window is the amount of data (in bytes) that the receiver is willing to accept at once. It prevents the sender from overwhelming the receiver.

How the Window Works

  • The receiver advertises a window size in each TCP segment.

  • The sender must not send more data than the receiver’s window without first receiving an ACK that frees up space.

  • This creates a flow control mechanism.

The Window Size Limitation

The window field is only 16 bits, limiting the raw value to 65535. With high-latency or high-bandwidth links, this small window drastically reduces throughput because the sender must stop and wait for ACKs.

Window Scaling (RFC 1323)

To overcome this limitation, the window scale option multiplies the advertised window by a factor of 2shift count2^{\text{shift count}}2shift count. The shift count is exchanged during the handshake. Typical shift counts:

  • Client: shift count 6 → multiply by 64 → true window ≈ 4 MB

  • Server: shift count 9 → multiply by 512 → true window ≈ 33 MB

In Wireshark, the Calculated window size column shows the true window (raw window × multiplier). Always use this value for analysis.

Dynamic Window Size

The window size is not static. As data arrives and is processed by the application, the buffer occupancy changes, and the window size fluctuates. A healthy TCP connection will see the window float around a common value. If the window drops to zero, it means the receiver’s buffer is full and the sender must stop sending — a condition called zero window.

  • A zero window is a red flag: the application is not consuming data fast enough or there is a stuck buffer.

  • Monitoring the calculated window size over time (using Wireshark’s graphing tools) is valuable for diagnosing slow transfers.

Example: Window Dynamics in a Trace

  • After the handshake, the window may appear small (e.g., 2052 raw) but with a multiplier of 64, the true window is 131328 bytes (~128 KB).

  • As data arrives, the true window drops slightly (e.g., to 129856) as the buffer fills.

  • Once the application consumes the data, the window returns to the full value.


Header Length and the Evolution of TCP

The TCP header length is a 4-bit field (units of 4 bytes). The minimum is 20 bytes; the maximum is 60 bytes. Options are squeezed into this space. Modern TCP options (window scale, SACK, timestamps) take up significant space. With only 40 bytes available for options, TCP is reaching its limits. This is one reason why new protocols like QUIC (which runs over UDP) are gaining traction — they avoid these header constraints.


Practical Wireshark Setup for TCP Analysis

To analyze TCP effectively, add these columns to Wireshark:

  • Delta time (displayed) – Time between consecutive packets. Helps measure round-trip time (RTT).

  • TCP segment length – How much payload data is in the segment (zero for pure ACKs).

  • Calculated window size – The true receive window after multiplier.

Adding Columns

  • Go to Edit → Preferences → Appearance → Columns (or right-click a column header and select “Column Preferences”).

  • Click “+”, then type the field name (e.g., Delta time displayed).

  • Choose the appropriate type from the dropdown.

Using the Display Filter

Wireshark’s filter bar is a powerful cheat sheet. Click on any protocol field in the packet details pane, and the corresponding filter expression appears in the status bar.


TTL and Distance Estimation

The Time to Live (TTL) field in the IP header can estimate the number of hops to a remote host. Common starting TTL values:

  • 64 – Most modern operating systems (Linux, macOS, Windows)

  • 128 – Older Windows versions

  • 255 – Many routers and some servers

If a packet arrives with TTL = 238, and the starting value was likely 255, then the host is 255−238=17255 - 238 = 17255−238=17 hops away. The Round Trip Time (RTT) from the handshake also provides latency information.


Summary of Key Takeaways

  • The three-way handshake is more than just SYN, SYN-ACK, ACK; it synchronizes sequence numbers and exchanges critical options (window scale, MSS, SACK).

  • Sequence numbers are per-direction and increment with every byte sent.

  • Acknowledgment numbers confirm receipt and allow the sender to free buffers.

  • The receive window, combined with window scaling, controls flow and is a major factor in throughput.

  • TCP options are only exchanged during the handshake; never skip capturing the handshake.

  • The calculated window size is the true window; the raw window (often small) is meaningless without the multiplier.

  • A zero window indicates a stuck receiver — a classic performance problem.

  • Understanding flags helps in filtering and analyzing connection states (SYN, ACK, FIN, RST, PSH, etc.).

  • TCP’s header is space-constrained, driving the shift toward QUIC for modern applications.

By mastering these fundamentals, network engineers, security analysts, and developers can quickly diagnose whether TCP itself is the problem or if the issue lies higher up the stack.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent