Introduction to TCP protocols, algorithms and principles

Things You Should Know About TCP (Part 1)

TCP is a highly complex protocol because it has to handle a wide range of issues, and each of these problems leads to many sub-problems and hidden challenges. Learning about TCP can be a tough process, but it also offers great rewards in terms of understanding network communication and software design. For a detailed look at the TCP protocol, I strongly recommend reading "TCP/IP Illustrated, Volume 1: The Protocols" by W. Richard Stevens. Of course, you can also refer to RFC 793 and other related RFCs for deeper insights. In this article, I’ll use English terminology so that you can easily search for technical documentation using these keywords.

There are three main reasons why I decided to write this article.

First, I want to test whether it’s possible to describe such a complex protocol in simple language.

Second, many programmers today don’t take the time to deeply study books and prefer quick, superficial knowledge. I hope this short read will help you understand the fundamentals of TCP and the design challenges behind it, while giving you some practical insights into software engineering.

Most importantly, I hope this basic knowledge helps you make sense of things you may have previously found confusing and highlights the importance of foundational knowledge.

Therefore, this article won’t cover everything—only the key concepts, algorithms, and principles of TCP.

Originally, I intended to write just one article, but TCP turned out to be more complicated than I expected—more complex even than C++. Over the past 30 years, numerous optimizations and variations have been debated and implemented. As a result, I ended up splitting the content into two parts.

In the previous part, I introduced the definition of the TCP protocol and its retransmission mechanism when packets are lost.

In this section, we’ll focus on TCP’s congestion control and connection management.

Let’s start with the basics. In the OSI seven-layer model, TCP operates at Layer 4—the Transport Layer. IP is at Layer 3—the Network Layer. ARP is at Layer 2—the Data Link Layer. The data at Layer 2 is called a Frame, at Layer 3 it's a Packet, and at Layer 4 it's a Segment.

To clarify, the data from your application first goes into a TCP Segment. Then, the Segment is encapsulated into an IP Packet, followed by an Ethernet Frame. Once it reaches the destination, each layer decapsulates the data and passes it up to the next higher layer.

TCP Header Format

Now let’s take a closer look at the structure of the TCP header.

Those things about TCP (on)

TCP header format (image source)

Key Points to Remember:

The TCP header does not include IP addresses—it’s handled at the IP layer. Instead, it contains the source and destination ports.

A TCP connection is uniquely identified by a four-tuple: (source IP, source port, destination IP, destination port). Sometimes this is considered a five-tuple when including the protocol, but since we’re focusing on TCP, we’ll stick with the four-tuple.

From the diagram above, there are four critical fields in the TCP header:

The Sequence Number is used to track the order of data segments and resolve packet reordering.

The Acknowledgment Number (ACK) confirms that a segment was received and helps prevent data loss.

The Window field, also known as the Advertised Window or sliding window, is used for flow control.

The Flags field indicates the type of segment and is crucial for managing the TCP state machine.

For other fields, please refer to the diagram below.

Those things about TCP (on)

TCP State Machine

It’s important to understand that network communication isn’t inherently connected. Even TCP, which is often referred to as a “connection-oriented” protocol, is actually just maintaining a “connection state” on both ends. This makes it appear as if a connection exists. Therefore, understanding the TCP state transitions is essential.

Below is the “TCP state machine” (picture source), along with a comparison chart showing the differences between establishing a connection, transferring data, and closing a connection. I’ve placed them side by side for easier reference. These two diagrams are very important—make sure to study them carefully. (Fun fact: seeing how complex the state machine is, you can begin to understand why TCP is so tricky. Complex systems always come with their share of pitfalls.)

Those things about TCP (on)

Many people wonder why TCP uses a three-way handshake to establish a connection and a four-way handshake to close it.

The three-way handshake is primarily used to initialize the sequence numbers on both sides. Each side sends its initial sequence number (ISN) to the other, which is used to ensure that data arrives in the correct order. This is what the SYN flag represents—short for "Synchronize Sequence Numbers." The numbers x and y in the diagram represent these ISNs. These values will be used as the starting point for all future data transmission, ensuring that the receiving application gets the data in the right order.

Outdoor Battery Cabinet

Outdoor Battery,Outdoor Powder Coating,Battery Rack,Integrated Rack

Guangdong Yuqiu Intelligent Technology Co.,Ltd , https://www.cntcetltd.com

This entry was posted in on