The sliding window

Sliding window is a flow control technique. In the early days of network communication, communication parties directly sent data regardless of network congestion. Because everyone did not know the network congestion, at the same time to send data, resulting in the middle node blocking switch, no one can send data, so there is a sliding window mechanism to solve this problem. (Baidu Encyclopedia)

In fact, the sliding window is a mutual negotiation, the data can not be sent more than the processing capacity of the other party.

  1. #1 represents data that has been sent and confirmed
  2. #2 represents data that has been sent but is not Ack
  3. #3 indicates that data that has not yet been sent is about to be sent
  4. #4 indicates that no data was sent

The black box is what we call a sliding window, which is 20 bytes in size. When #2 Ack comes back, it will return the window size of the other party, and then the sender will adjust it dynamically.

The figure above shows the actual execution of the sending and receiving Windows. Interestingly, the window is full (window=0), which is usually reset by the RST flag bit. That’s out of the scope of this discussion.

In the figure above, we can see that the Server recalculates the RCV.WND value according to the data buffer it consumes. The ACK response will carry the window field to the Client, and the Client will carry the window field according to the window size of the Server. Resize the send window.

Nagle algorithm and delayed validation

Naggle

However, if TCP packet communication is carried out frequently, the network efficiency is very low. For the sender, we can use Nagle algorithm to improve the transmission efficiency

The Nagle algorithm sends the next data only when it receives an ACK message for the previous data or when the MSS value is reached after a timeout of 40ms. The Nagle algorithm used by TCP sockets to exchange data by default is therefore buffered to the maximum until an ACK is received.

You can turn off the Naggle algorithm with the TCP_NODELAY parameter.

TCP delayed ack

The recipient does not reply to the ACK immediately after receiving the data. Instead, the ACK is delayed for a certain amount of time or until the maximum data length is 2x (different operating systems implement this differently).

  1. In this way, ACK packets can be combined. That is, if two CONSECUTIVE TCP packets are received, you do not need to ACK them twice, but only need to reply the final ACK, which reduces network traffic.
  2. If the receiver has data to send, it carries an ACK message in the TCP packet that sends the data. In this way, a large number of ACKS can be sent as a single TCP packet, reducing network traffic.

Reference Documents:

www.tcpipguide.com/free/t_TCPS…

More highlights of the public account (Dull Bear technology road):