Chapter 23 of 28

TCP Connection & Flow Control

TCP is designed to provide reliable communication between two applications over a network. But before TCP starts sending application data, it needs to establish a connection. It also needs to make sure that a fast sender does not overwhelm a slower receiver.

These two important concepts are called TCP Connection Management and TCP Flow Control.

In simple terms:

TCP Connection → Establishes communication between two endpoints.

TCP Flow Control → Makes sure the sender does not send more data than the receiver can handle.

Let's understand both step by step.


TCP Connection

TCP is a connection-oriented protocol. This means a connection is established before application data is normally exchanged.

For example, when a client wants to communicate with a server using TCP, they first perform a three-way handshake.

Client                         Server

   |────── SYN ───────────────→|
   |←──── SYN + ACK ───────────|
   |────── ACK ────────────────→|
   |
   |   Connection Established

The three messages are:

  1. SYN – The client requests a connection.

  2. SYN-ACK – The server acknowledges the request and responds.

  3. ACK – The client acknowledges the server's response.

After this process, both sides are ready to exchange application data.


Why Does TCP Need a Handshake?

The handshake allows both sides to establish important state for the connection, including their initial sequence numbers.

TCP uses sequence numbers to keep track of bytes in the data stream.

For example:

Client → Server
Data with sequence information

Server → Client
Acknowledgment

This helps TCP determine which data has been received and which data may need to be retransmitted.


TCP Data Transfer

Once the connection is established, data can be exchanged.

A simplified example looks like this:

Client                         Server

   |──── Data ────────────────→|
   |←──── ACK ─────────────────|
   |──── More Data ───────────→|
   |←──── ACK ─────────────────|

The acknowledgment tells the sender how much data has been successfully received.

TCP can send multiple segments before receiving all corresponding acknowledgments. This is more efficient than sending one segment and waiting every time.


What Is TCP Flow Control?

Imagine a powerful server sending data very quickly to a small device.

The sender may be capable of transmitting data much faster than the receiver can process it.

If the sender keeps sending without considering the receiver's available buffer space, the receiver's buffer could become full.

TCP flow control prevents this situation by allowing the receiver to tell the sender how much additional data it can accept.

Fast Sender ─────────→ Slow Receiver
             ↓
        Flow Control
             ↓
     Adjust sending amount

TCP Receive Window

TCP uses a receive window, commonly called the advertised window, for flow control.

The receiver advertises how much data it can currently accept without requiring the sender to wait for the application to process more data.

For example:

Receiver says:

"Window = 20 KB"

The sender can have up to approximately that amount of unacknowledged data in flight, subject to other TCP limitations.

If the receiver has less available buffer space:

"Window = 5 KB"

the sender needs to reduce the amount of outstanding data.


Sliding Window

TCP uses a sliding-window mechanism to allow multiple bytes of data to be in transit at the same time.

Suppose the sender is allowed to have four units of data outstanding:

[1] [2] [3] [4]  5  6  7
 ↑────────────↑
   Sendable Window

After the receiver acknowledges some data, the window moves forward:

  1  2  [3] [4] [5] [6]  7
       ↑──────────────↑
          New Window

This allows TCP to keep the network busy instead of stopping after every segment.


Flow Control Example

Suppose a receiver has room for 10 KB of additional data.

It can advertise a receive window representing that available capacity.

The sender then limits the amount of unacknowledged data accordingly.

Later, the receiving application processes some data and more buffer space becomes available.

The receiver can advertise a larger window.

Buffer Available: 10 KB
        ↓
Receive Window: 10 KB
        ↓
Sender adjusts transmission

Later...

Buffer Available: 30 KB
        ↓
Receive Window: 30 KB
        ↓
Sender can send more

This allows TCP to adapt to the receiver's current capacity.


Flow Control vs Congestion Control

This is a very important distinction.

Both mechanisms can affect how much data TCP sends, but they solve different problems.

Flow Control

Congestion Control

Protects the receiver

Protects the network

Based mainly on receiver capacity

Based on network congestion

Uses receive window

Uses congestion-control mechanisms

Prevents receiver buffer overflow

Helps avoid overwhelming the network

Think of it like this:

Flow Control → "My receiver can't handle more."

Congestion Control → "The network can't handle more."

TCP considers both when determining how much data can be in flight.


TCP Connection Termination

TCP also needs a way to properly close a connection.

A normal TCP connection termination commonly involves FIN and ACK messages and usually takes multiple steps because TCP connections are full-duplex.

A simplified view is:

Client                         Server

   |──── FIN ────────────────→|
   |←──── ACK ─────────────────|
   |←──── FIN ─────────────────|
   |──── ACK ────────────────→|

This allows each direction of the connection to be closed independently.


TCP Connection Lifecycle

The overall TCP process can be simplified as:

Connection Establishment
          ↓
     Data Transfer
          ↓
Connection Termination

More specifically:

SYN
 ↓
SYN-ACK
 ↓
ACK
 ↓
Data Exchange
 ↓
FIN / ACK
 ↓
Connection Closed

Why Are Connection and Flow Control Important?

Without connection management, TCP would not have a well-defined process for establishing and terminating communication.

Without flow control, a fast sender could overwhelm a slower receiver.

Together, these mechanisms help TCP provide reliable and controlled communication.

Conclusion

TCP connection management and flow control are two important parts of TCP.

TCP connection management establishes communication using the three-way handshake and later terminates the connection using mechanisms such as FIN and ACK.

TCP flow control prevents a sender from overwhelming the receiver by using the receive window and sliding-window mechanism.

The easiest way to remember the concepts is:

Three-Way Handshake → Establish the connection

Sequence Numbers & ACKs → Track received data

Receive Window → Control how much data the receiver can accept

FIN/ACK → Close the connection

And remember the key difference:

Flow Control protects the receiver, while Congestion Control protects the network.