What is the use of TCP's sliding window protocol?

Analysis & Answers

Sliding window protocol:

  • Use of TCP protocol
  • Maintain the sender/receiver buffer.
    The buffer is used to solve the problem of unreliable data between networks, such as packet loss, duplicate packets, errors, and out-of-order

In the TCP protocol, the sender and receiver each maintain their own buffers. The problem of unreliability is solved by agreeing on a series of operations such as the retransmission mechanism of the packet.

Question 1: How to ensure the order?

Ask the question: Before our sliding window protocol, how can we ensure that every packet between the sender and the receiver can be received. And in order?

The sender sends a packet 1, and the receiver acknowledges packet 1. Send package 2, confirm package 2. Just keep going like this until the data is completely sent, and it's over. Then it solves packet loss, errors, disorder and other situations! There are also some problems. Problem: Throughput is very low. After we send package 1, we must wait for confirmation of package 1 before we can send the second package.

Question 2: How to improve throughput?

Asking a question: So can’t we send a few packages first and wait for him to confirm them together? In this case, will our speed be faster and the throughput higher?

As shown in the picture, this means we send the two packages together and then confirm them together. It can be seen that our improved solution is much better than the previous one, and the time it takes is just a round trip. Next, we have another problem: the problem of improving throughput

Question 3: How to achieve the optimal solution?

Question: How many packages do we need to send each time? How many packets to send is the optimal solution?

Can we send the first and second packets and then send the third packet after receiving the first confirmation packet? Instead of waiting for the confirmation package of the second package before sending the third package. This naturally leads to the implementation of our "sliding window".

In the picture, we can see that the gray packet No. 1, No. 2, No. 3 has been sent and the Ack has been received. These bags are a thing of the past. Packets No. 4, 5, 6, and 7 are yellow, indicating that they have been sent. But I didn't receive the Ack from the other party, so I don't know if the receiver has received it. Bags No. 8, 9, and 10 are green. That we haven't sent yet. These greens are the packages we will send right away. It can be seen that our window is exactly 11 grids. The following 11-16 have not been read into the memory. Our packets will not continue to be sent until packets No. 4-10 have the next action.

normal circumstances

You can see that the other party of packet No. 4 has been received, so it is painted gray. Move the "window" one space to the right. Just make sure that the "window" is 7 spaces. We read packet No. 11 into our cache. Entered "to be sent" status. Packages 8 and 9 have turned yellow, indicating that they have been sent. The next operation is the same. After confirming the package, the window moves back and continues to read unsent packages into the cache, changing the "to-be-sent" status of the package to "sent".

Packet loss

It's possible that we sent the package and the other party's Ack was lost. It is also possible that our package was not sent. From the sender's perspective, we did not receive the Ack.

What happened: Waiting for Ack. If we still can't wait, we will also send the packets that have been read into the cache and are to be sent. However, our window is full at this time. Therefore, packet No. 12 cannot be read in, but it is always waiting for the Ack of packet No. 5.

What if our Ack never comes?

Timeout and resend

At this time we have a solution: 超时重传
There is one thing to note here: this Ack must be in order. You must wait until the Ack of 5 is received before sending the Ack of 6-11. This ensures an order for the sliding window.

At this time, it can be seen that the Ack has been received for packet No. 5, and the subsequent packets No. 6, 7, and 8 have also been sent and Ack'd. The window continues to move backward.

Meow Interview Assistant: A one-stop solution to interview questions. You can search the WeChat applet [Meow Interview Assistant]  or follow [Meow Interview Assistant] -> Interview Assistant to  answer questions for free. If you have any good interview knowledge or skills, I look forward to sharing them with you!

Guess you like

Origin blog.csdn.net/jjclove/article/details/127392409