TCP-IP: Sliding windows (sliding window)

Source: Internet
Author: User
Tags ack

Reference books: Tcp-ip Guide


Advantages of TCP

From the transmission of data, TCP/UDP and other protocols can be transmitted to the data, from one end of the transmission to the other side, TCP is superior to provide a reliable, flow-controlled data transmission, so the implementation is more complex than other protocols, Let's look at the meaning of these two modifiers:

1. Reliability, to provide TCP reliability, TCP transmission to ensure that the data can be accurately reached the destination, if not, need to be able to detect and resend data.

2. Data flow control , to provide TCP streaming characteristics, to manage the rate of data transmission, do not exceed the load capacity of the equipment

In order to achieve the above 2 points, TCP realizes a lot of details of the function to ensure data transmission, such as sliding window adaptive system, timeout retransmission mechanism, cumulative ACK, etc., this time to introduce a sliding window of some knowledge points.


Sliding Window Introduction

When reading some articles to see a video of Daniel, very good easy to understand the mechanism of sliding window, you can first look at: http://v.youku.com/v_show/id_XNDg1NDUyMDUy.html

IP layer protocol is an unreliable protocol, IP layer does not relate to the data is sent to the End-to-end, TCP through the validation mechanism to ensure the reliability of data transmission, in the early days of the use of send--wait--send mode, In fact, this mode is called stop-wait mode, sending data will start the timer after sending data, but if the data or ACK is lost, then the timer expires, not receive an ACK is considered to send the situation, to be retransmission. This reduces the efficiency of the communication, as shown in the following illustration, which is called positive acknowledgment with retransmission (PAR)



sliding window

Let's assume, to optimize the low efficiency of par, for example, I have to send each packet has an ID, the receiver must be confirmed for each package, so that device a more than one time to send a few pieces, and do not have to wait for an ACK, while the receiver also told it can receive how much, so that the sending end also has a limit, Of course, also need to ensure that the order, not disorderly, for disorderly order of the situation, we can allow to wait for a certain situation of chaos, such as the first buffer to the data before, and then to wait for the required data, if a certain time did not drop off to ensure that the order of nature.

In the TCP/IP protocol stack, the introduction of sliding window can solve this problem, first of all, to look at the concept of the data into which classes

1. Sent and acknowledged: These data represent the data that has been sent successfully and have been validated, such as the first 31 bytes in the picture, where the data is actually located outside the window, because the lowest order in the window is confirmed to remove the window, which is actually a window closure. Open to receive new sent data at the same time

2. Send But not yet acknowledged: this part of the data is referred to as sending but not confirmed, the data is sent out, did not receive the ACK of the receiver, think and did not complete the send, this belongs to the window of the data.

3. Not sent,recipient Ready to Receive: This part is the data sent as soon as possible, this part of the data has been loaded into the cache, that is, the window, waiting to be sent, in fact, this window is fully informed by the receiver, the receiver told or can accept these packets, So the sender needs to send these packages as soon as possible.

4. Not sent,recipient-Ready to receive: These data are not sent and are not allowed to be sent at the receiving end, because the data is beyond the range received by the sending end


For the receiver there is also a receive window, similar to the sender, the receiving end of the data has 3 categories, because the receiver does not need to wait for ACK so it does not have a similar received and confirmed the classification, the situation is as follows

1. Received and ACK not sent to Process: This part of the data belongs to the received data but has not been received by the upper level of the application, is also cached in the window

2. Received not ACK: has received and, but has not replied ACK, these packets may lose belong to the category of delay ACK

3. Not Received: There is no vacancy, no data has been received.

Send window and available window

For the sender, the window includes two parts, that is, the Send window (has been sent, but did not receive an ACK), available windows, the receiving side allows to send but did not send the part called the available window.

1. Send window:20 A bytes this part of the value is the recipient of the three handshake when the notification, while in the process of receiving the notification can be sent to the size of the window to adapt

2. Window already Sent: Data that has been sent, but does not receive an ACK.


sliding window Principle

TCP is not every message segment will reply Ack, may send an ACK to two message segments, may also send 1 ack " cumulative ack" to multiple message segments, for example, the sender has 1/2/3 3 message segments, first sent 2, 32 message segments, But the receiver expects to receive a 1 message segment, this time 2, 3 message segments can only be placed in the cache waiting for message 1 hole is filled, if the message 1, has not come, message 2/3 will also be discarded, if the message comes, then send an ACK to the 3 messages to confirm.

Give an example to illustrate the principle of sliding windows:

1. Suppose 32~45 this data is sent to TCP by the upper application, TCP divides it into four segment to send to the Internet

2. SEG1 32~34 seg3 35~36 seg3 37~41 seg4 42~45 These four fragments are sent out in turn, assuming that the receiving end received SEG1 SEG2

3. At this time the receiver's behavior is to reply to an ACK package that has received the 32~36 data, and the SEG4 cache (guaranteed order, produce a save seg3 hole)

4. When the sender receives an ACK, it will 32~36 the packet from the sending and not confirm the cut to send has been confirmed, proposed window, this time the window to the right to move

5. Assuming that the window size of the receiving notification is still unchanged, the windows are shifted to the right to create new vacancies, which are the scope of the receiving side to allow sending

6. For lost Seg3, if over a certain period of time, TCP will retransmit (retransmission mechanism), retransmission success will be seg3 SEG4 piece is confirmed, unsuccessful, SEG4 will be discarded

is to repeat the above process, as the window continues to slide, the entire flow of data to the receiver, in fact, the receiving end of the window size notice will also change, the receiver based on this value to determine when and how much data, from the stream of data flow control. The schematic diagram shows the following figure:



sliding window Dynamic adjustment

The main is based on receiving the receiver, dynamic to adjust the window Size, and then to control the data flow at the end of the transmitter

1. The client sends the data quickly, the server receives relatively slowly, looks at the result of the experiment

A. Package 175, send ack carry win = 384, tell the client, now can receive only 384 bytes

B. Package 176, the client really only sent 384 bytes, Wireshark is also more intelligent, also announced that the TCP Window full

C. Package 177, the server replies to an ACK, and the notification window is 0, indicating that the receiver has received all the data and saved to the buffer, but this time the application did not receive this data, resulting in buffer no more space , so the notification window is 0, which is called 0 window , 0 window, sender stops sending data

D. When the client perceives a window of 0, it no longer sends data to the receiver

E. Package 178, the receiver sends a window notification informing the sender that it has the ability to receive data, and can send a packet.

F. Package 179, after receiving the window notification, send the data in the buffer.


To sum up, the receiver can announce the window size according to its own situation, thus controlling the receiving of the transmitter and carrying out flow control


Reference Articles

1.http://www.cricode.com/2679.html

2.http://kb.cnblogs.com/page/209100/



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.