Things You Should Know About TCP (Part 1)
TCP is one of the most complex protocols in networking, as it was designed to handle a wide range of challenges—like packet loss, reordering, congestion, and reliability. These issues led to many sub-problems and complicated solutions over time. Learning TCP can be tough, but it's also very rewarding, especially if you're interested in how software systems work at a deep level. For those who want to dive deeper, I highly recommend reading "TCP/IP Illustrated, Volume 1" by W. Richard Stevens. You can also look into RFC 793 and other related RFCs for more technical details. Since this article uses English terminology, it will help you search for relevant resources online using the right keywords.
There are three main reasons why I decided to write this piece. First, I wanted to challenge myself to explain such a complex protocol in a concise way. Second, many programmers today prefer quick reads over in-depth study. This article is meant to serve as a fast-food guide that still gives you real value, helping you understand classic TCP concepts and the design behind it. Finally, I hope this knowledge helps you make sense of things you've seen before and appreciate the importance of solid foundational knowledge.
That said, this article won’t cover everything. It focuses on the core principles, algorithms, and mechanisms of TCP. Initially, I planned to write just one article, but TCP turned out to be even more complicated than C++. Over the last 30 years, countless optimizations and debates have made it even more intricate. So, I split it into two parts.
In the first part, I introduced the basics of TCP, including its purpose and the retransmission mechanism when packets are lost. In this second part, we’ll focus on TCP’s congestion control and connection management.
Let’s start with the basics. TCP operates at the transport layer in the OSI model, while IP is at the network layer. ARP is at the data link layer. When data moves through the network, it gets encapsulated differently at each layer: frames at Layer 2, packets at Layer 3, and segments at Layer 4.
The data from your application is first wrapped into a TCP segment, then passed to the IP layer as a packet, and finally sent as an Ethernet frame. On the receiving end, each layer decapsulates the data and passes it up to the next higher layer.
TCP Header Structure
Now let’s take a closer look at the TCP header.
TCP Header Format (Image Source)
Key Points to Note:
The TCP header doesn’t contain IP addresses—it’s handled by the IP layer. Instead, it includes source and destination ports. A TCP connection is uniquely identified by a four-tuple: (source IP, source port, destination IP, destination port). Adding the protocol makes it a five-tuple, but since we’re focusing on TCP, we'll stick to the four-tuple.
Looking at the header, there are four critical fields:
- Sequence Number: Used to track the order of data segments.
- Acknowledgment Number: Indicates which data has been received successfully.
- Window Size: Also known as the advertised window, used for flow control.
- Flags: Control bits that manage the state of the connection.
Here’s a more detailed breakdown of the header.
The TCP State Machine
TCP isn’t actually a “connection†in the traditional sense. Instead, it maintains a “state†on both ends of the communication, making it appear as though a connection exists. This state machine is crucial for understanding how TCP works.
Below is the TCP state machine, along with a comparison chart showing the differences between establishing a connection, transferring data, and closing it. These two diagrams are essential for understanding TCP’s behavior. (Note: The complexity of the state machine shows just how intricate TCP really is. With so many states and transitions, it’s easy to fall into pitfalls if you don’t fully understand it.)
Many people ask: Why does TCP use a three-way handshake to establish a connection and a four-way handshake to close it?
The three-way handshake is used to exchange initial sequence numbers (ISNs), ensuring both sides agree on the starting point of the data stream. This is done through the SYN flag. The ISN is essentially a random number that ensures data arrives in the correct order. This helps prevent issues like duplicate or out-of-order packets.
Solar Energy Storage,Battery System,Battery Structure,Lifepo4 Battery Cells
Guangdong Yuqiu Intelligent Technology Co.,Ltd , https://www.cntcetltd.com