TCP/IP Study Notes (8) – TCP Protocol

TCP/IP Study Notes (8) - TCP Protocol

Finally, I have come to the TCP protocol, which is the most important and exciting part of TCP/IP detailed explanation, and it requires significant effort to read.

Brief Introduction

TCP and UDP are at the same layer— the transport layer. However, the most significant difference between TCP and UDP is that TCP provides a reliable data transmission service. TCP is connection-oriented, which means that the two hosts communicating using TCP must first go through a “dialing” process before starting to transmit data, and only after the communication is ready does the data transmission begin. Therefore, TCP is much more reliable than UDP; UDP sends data directly without caring whether the other party is receiving it. Even if UDP fails to deliver, it does not generate ICMP error messages, which has been reiterated many times.

Working Principle

Here is a simple excerpt of how TCP ensures reliability:

  1. Application data is divided into data blocks that TCP considers most suitable for sending. This is entirely different from UDP, where the length of the datagram generated by the application remains unchanged. The information unit transmitted by TCP to IP is called a segment.

  2. When TCP sends a segment, it starts a timer and waits for the destination to confirm receipt of this segment. If it does not receive a confirmation in time, it will retransmit this segment.

  3. When TCP receives data from the other end of the TCP connection, it sends an acknowledgment. This acknowledgment is not sent immediately and is usually delayed by a fraction of a second.

  4. TCP maintains a checksum for its header and data. This is an end-to-end checksum designed to detect any changes to the data during transmission. If the checksum of the received segment is incorrect, TCP will discard this segment and not acknowledge its receipt (hoping that the sender will timeout and retransmit).

  5. Since TCP segments are transmitted as IP datagrams, and the arrival of IP datagrams may be out of order, the arrival of TCP segments may also be out of order. If necessary, TCP will reorder the received data and deliver it to the application layer in the correct sequence.

  6. TCP can also provide flow control. Each side of a TCP connection has a fixed-size buffer. The TCP receiver only allows the other side to send data that the receiver’s buffer can accommodate. This prevents a faster host from overflowing the buffer of a slower host.

From this passage, we can see that the way TCP maintains reliability is through timeout retransmission. This makes sense; although TCP can also use various ICMP messages to handle these, it is not reliable. The most reliable method is to retransmit the datagram until confirmation is received.

TCP Sending Process

The TCP header, like the UDP header, has the sender’s and receiver’s port numbers. However, the TCP header contains much more information than the UDP header. It can be seen that the TCP protocol provides all the necessary information required for sending and acknowledgment. One can imagine that the process of sending TCP data should be as follows:

  1. Both parties establish a connection.

  2. The sender sends a TCP datagram to the receiver and then waits for the acknowledgment of the TCP datagram. If there is none, it retransmits; if there is, it sends the next datagram.

  3. The receiver waits for the sender’s datagram. If it receives the datagram and verifies it is correct, it sends an ACK (acknowledgment) datagram and waits for the next TCP datagram to arrive until it receives a FIN (finish sending datagram).

  4. Terminate the connection.

It can be imagined that to establish a TCP connection, the system may establish a new process (at worst, a thread) to carry out data transmission.

Let every lesson you learn be fruitful

“Linux Should Be Learned This Way” is a high-quality self-learning tutorial on Linux technology co-authored by senior operation and maintenance expert Liu Chuan and several Red Hat architects (RHCA) based on the latest RHEL7 system, which is extremely suitable for Linux technology introduction or as a teaching aid. It has won the championship in the IT category book sales during the Double 11 and Double 12 shopping festivals and is the fastest-growing technical book among domestic readers in 2017 and 2018. You can search for the book title on JD, Dangdang, Amazon, and Tmall to purchase it, or add Liu Chuan’s WeChat for learning exchanges (press and hold the image below for 3 seconds to scan automatically)~

TCP/IP Study Notes (8) - TCP Protocol

Liu Chuan’s QQ: 5604215

Linux Technology Exchange Group: 560843New group, hotly joining the group…

Official Site: www.linuxprobe.com

☀ Online Learning of Books (better reading effect on computer):

http://www.linuxprobe.com/chapter-00.html

“Linux Should Be Learned This Way” is a technical book written based on the latest Linux system, aimed at zero-based readers. It starts with the basics of Linux and gradually increases the difficulty of the content, providing detailed explanations of the working principles and configuration methods of various services in the Linux system to meet the requirements of operations personnel in a real production environment, highlighting the practicality of the content. Readers who want to learn the Linux system can click the “Read the Original” button to learn more about this book. This book is also suitable for professional operations personnel to read as a highly valuable reference book!

Leave a Comment