An article to understand the sliding window protocol
An article to understand the sliding window protocol
Yesterday we briefly talked about HTTP and HTTPS. Why is it simple? Because it is the explanation of the basic HTTP protocol and the security of HTTPS, etc., some readers said, why not talk about some advanced content.
stop waiting agreement
Before we understand the sliding window protocol, we must first understand what is the stop and wait protocol, and how does the stop and wait protocol work?
The stop-and-wait protocol (stop-and-wait) is the simplest but also the most basic data link layer protocol. Many basic concepts about the protocol can be learned from this protocol.
Stop waiting is to stop sending every time a packet is sent, waiting for the confirmation of the other party. Send the next packet after receiving the acknowledgment.
In short, after the sender sends the data packet, if there is no confirmation from the other party, it will wait forever without sending the next data packet, and continue to send the data packet after receiving the confirmation.
picture
This also leads to the obvious disadvantages of the stop-waiting protocol.
- Sending only one packet at a time is inefficient.
The inefficiency is unimaginable. For example, if our bandwidth is 100M and the stop-and-wait protocol only sends one data packet each time, this is a waste of bandwidth, not to mention that many of them have a bandwidth of more than Gigabit. .
Because of this efficiency problem, the stop-and-wait protocol is not very applicable, so other protocols appear, which is the sliding window protocol we will talk about next.
Sliding window protocol
So what is a sliding window protocol?
Sliding Window Protocol (Sliding Window Protocol), an application of the TCP protocol, is used for flow control during network data transmission to avoid congestion. The protocol allows the sender to send multiple data packets before stopping and waiting for an acknowledgment. Because the sender does not have to stop and wait for confirmation every time a packet is sent. Therefore, the protocol can accelerate data transmission and improve network throughput.
In the sliding protocol, the sender needs to maintain a sending window. With the transmission of data, this window needs to slide forward continuously. This is different from the stop-and-wait protocol. What is the difference?
The difference is that it allows the sender to send multiple data packets before stopping and waiting for confirmation, instead of just sending one data packet each time like the stop-and-wait protocol. In this way, the sender does not need to stop and wait every time it sends a data packet. This is the most essential difference between them.
At this time, readers have questions, how many packets will be sent at this time?
The number of packets depends on a parameter, and this parameter is called the window size.
Let's simply simulate the situation where data does not lose packets under this sliding window protocol.
picture
In the figure above, the window size is 4, and our sender has 10 data packets to send, which means that we can send four data packets at a time,
picture
When the sender sends the first packet, does the sliding window start running at this time? Indeed, when we send the first data packet, the sliding window starts to run, which means that we can send data packets with a window size of 4 before receiving the confirmation.
After the No. 3 data packet is sent, the corresponding confirmation messages of No. 0-3 are also fed back to the sender.
At the same time, the window starts to slide to the left one after another.
picture
We can also see from the figure that the parts that have been sent, are being sent and waiting to be sent are distinguished.
The principle of the sliding window protocol can be seen as follows:
The main principle of the sliding window protocol is to identify each data packet by using a sequence number, and use the acknowledgment number to confirm the received data packet. The sender maintains a send window of packets that have been sent but not acknowledged. The receiver maintains a receive window of packets received but delivered out of order.
When sending a data packet, the sender adds the sequence number of the data packet to the data packet and sends it to the receiver. After receiving the data packet, the receiver adds the confirmation number to the confirmation packet and sends it to the sender. After receiving the confirmation packet, the sender slides the sending window forward, removes the confirmed data packet from the sending window, and continues to send the next data packet.
If the sender does not receive an acknowledgment packet within a certain period of time, or the receiver does not receive a correct data packet within a certain period of time, the sliding window protocol will trigger a timeout retransmission mechanism to resend unacknowledged or incorrectly received data packets.
The sliding window protocol can improve the efficiency and reliability of data transmission while making full use of network bandwidth. It is widely used in various network communications, such as the TCP protocol is implemented based on the sliding window protocol.
Points to note in the sliding window protocol
(1) The sender does not have to send a full window of data. (2) A message segment from the receiver confirms the data and slides the window to the left, because the size of the window is relative to the confirmation sequence number. (3) The size of the window can be reduced, but the left edge of the window cannot move to the right. (4) The receiver does not have to wait for the window to be filled before sending an ACK.
The right and left depends on the direction of window movement when you understand the picture. I am used to going from right to left, and you can also understand it as going from left to right. The understanding is the same.
So, do you understand the sliding window protocol?