Why reliable TCP protocol?

Why reliable TCP protocol?

We all know the difference between TCP and UDP protocols is that TCP provides reliable data transmission network, but UDP does not.

Why TCP is more reliable it? Some may answer, TCP is a connection-oriented, rather than UDP. What connection is it? It is a thing like the same pipe, connected to all the data in one place ensures data integrity and orderly reach it? Well, not exactly.

TCP / IP Detailed have a detailed description of the entire data transmission process. I tried the key in which the extracted part, explain the TCP protocol in the end is how to ensure the reliability of the transmission.

establish connection

What connection is

When it comes to connections, we would instinctively think of it as a pipe or rope, to establish a connection is to pick up the hose or rope. When communicating, we will connect to imagine an independent channel, all data transmitted along this path out after the connection is established, the other party will be able to receive the complete data orderly. But in fact, due to a variety of problems (hardware failures, network congestion, attacks, etc.) in the presence of network transmission channel itself is not reliable.
Therefore, TCP connection is not in the so-called a channel, it is only logical to establish the relationship between the two sides of a one to one communication, so that both sides are clearly other is their communication goals.

Since both sides agreed there is only logic, data may still be various error conditions, such as loss occurs during transmission. What is the significance of establishing a connection that is it? for example:

If at first the two sides did not establish that a short communication connections B A in the end do not know what to do, do not know the contents of A said from somewhere to somewhere to start over, and B will no law to ensure the complete and correct communication .

More details can refer to in the end what is a TCP connection?

Three-way handshake to establish a connection

TCP three-way handshake to establish a connection by way of a specific procedure shown below:

Can be seen from the figure, the three-way handshake process is actually a client and server send each other a number to seq, and the other received ACK (received seq + 1) of the process. Data in the second handshake seq contains the server to the client and the server sends the ACK.
After the three-way handshake, the two sides are connected at the same time to enter the ESTABLISHED(连接成功)state, ready to begin data transfer.

如果想知道三次握手建立连接,或者四次挥手断开连接的更多细节,可以看看简析TCP的三次握手与四次分手这篇文章。
另外,知乎上为什么TCP是三次握手,而不是两次或者四次握手这个问题,解释了三次握手的设计初衷,值得一看。

传输数据

ACK机制

由于通信过程的不可靠性,传输的数据不可避免的会出现丢失、延迟、错误、重复等各种状况,TCP协议为解决这些问题设计了一系列机制。
这个机制的核心,就是发送方向接收方发送数据后,接收方要向发送方发送ACK(回执)。如果发送方没接收到正确的ACK,就会重新发送数据直到接收到ACK为止。
比如:发送方发送的数据序号是seq,那么接收方会发送seq + 1作为ACK,这样发送方就知道接下来要发送序号为seq + 1的数据给接收方了。

我们来看看在不同的异常情况下,ACK机制是怎么工作的:

  • 数据丢失或延迟。发送方发送数据seq时会起一个定时器,如果在指定时间内没有接收到ACK seq + 1,就把数据seq再发一次。
  • 数据乱序。接收方上一个收到的正确数据是seq + 4,它返回seq + 5作为ACK。这时候它收到了seq + 7,因为顺序错了,所以接收方会再次返回seq + 5给发送方。
  • 数据错误。每一个TCP数据都会带着数据的校验和。接收方收到数据seq + 3以后会先对校验和进行验证。如果结果不对,则发送ACK seq + 3,让发送方重新发送数据。
  • 数据重复。接收方直接丢弃重复的数据即可。

ACK的优化

按照ACK机制,只要整个数据传输顺利结束,接收方就能收到完整有序的数据了。但是,如果我们针对每一个数据包都发送ACK,就会有大量的网络资源消耗在ACK的发送上,这不太划算的。于是,TCP设计了延迟ACK的机制。

这个机制其实很简单。客户端一次给服务器发送多个数据包,当服务器收到客户端的数据包时,不马上发送ACK,而是稍微等一小段时间。在这个过程中服务器可能能收到后续几个数据包,服务器就可以直接按照最后一个正确的数据发送ACK,减少发送ACK的总数。

除了延迟ACK的机制,TCP还做了很多对传输过程的优化,比如滑动窗口机制,比如慢启动机制。由于跟本文的主题无关,我就不在这里多说了,有兴趣的同学可以搜索来看看。

Guess you like

Origin blog.csdn.net/weixin_42187898/article/details/94754395