Intuitive summary of the TCP sliding window mechanism principle and function

A. Principle

  TCP is a full-duplex communication, so that each side of the sliding window comprises a receive window + the send window, the receiving window is responsible for processing the data sent to it, the transmission window is responsible for processing data to be sent to its own. Slip In fact, the essence of dynamic window is to maintain several variables, these variables through the TCP data processing is divided into several categories, while sending a message, receiving a message to do some processing on these variables maintenance .

 

 

  FIG transmitting window above

(1) N is the start byte of the send window, that is to say: byte sequence number <N bytes have been sent and received ACK, the confirmation;

(2) nextSeq is next transmission packet header field Seq (Seq i.e. the first byte of the serial number, which do not speak here), represents the byte sequence number [N, nextSeq) section have been used, the transmission out, but ack not received confirmation;

(3) N + size sequence number is the last available byte window, size is the size of the send window, Win is the value of each field in the received packets, in fact, the size of the other fields Win receive window.

 How to make these values ​​to maintain it?

(1) a receipt of each packet to do the following things: Check received messages ack, ack N is set, i.e., moved forward ack value; Win read packet field value, i.e., the other side the latest received window size, thereby updating the value of size + N.

(2) to send a message each, to change the value of nextSeq, put the number of bytes sent nextSeq much moved forward, but do not exceed N + size.

 

See below the receiving window :

 

Also the maintenance of several variables on the number of bytes, the transmission window is similar except that the receiving window is received byte number of bytes. Meaning of several variables as follows:

(1) J1 represents: byte sequence number <J1 byte has been received, i.e. ack has been issued confirmed, the program can be said to be used.

(2) J2 represents: [Jl, J2) has full byte interval, the order is received, the program ready for reading, but only ACK has not yet issued, to confirm .

(. 3) may receive J3 is the last byte of a receive window, the bytes sent over J3 is to refuse to accept (generally do not receive, because the size of transmitting window is a window to the reception impossible back over ).

 How to maintain it?

(1) When sending a message: the value of J2 fill ack field in the message header, and then move J1 to J2, tell them you sent is less than the number of bytes that J2, I have received and correctly use a; the (J3-J2) of the difference in headers Win field, I have to tell each other how much space can be received.

(2) a packet is received, if no error message sequence, then the mobile J2, how many bytes received moves. If packets arrive out of order, Seq value in a received packet should be equal to J2, if not equal indicates that the intermediate packets are lost, and do not move J2, so the next ack message has been sent with a : J2, which is repeated to confirm, there are corresponding fast retransmission (pull away, not explained in detail).

3. sliding window principle summary

     Because the starting value of the window will slowly increase after the start, that is right, so this is the origin of the sliding window name. In fact, receive messages, send messages to maintain the value of several variables on ACK, the byte sequence number, SEQ (start byte packet number). About ack, seq, WinSize summarized as follows:

(1) Send message ack is how come, when the received message is ack how to use:

   From the reception window to take the value of J2 is filled ack packet header when sending a packet; transmitting window to get ack after receiving a packet start value N is updated to ack.

(2) how to send packets to the seq is, when the received message is how to use seq:

           When sending messages get sent from the window to fill value nextSeq seq packet header field; if the received message to view the message field is the receive window seq J2, according to the packet length is the continuous right J2 .

(3) transmitting packets is how to Win, Win when the received message is how to use:

          When sending a packet to get from the reception window (J3-J2) to fill this difference Win header field; get value Win header field when the message is received, assuming the end of size, updating of the transmitting window position N + size

 

ROLE

    We know, in fact, is a network layer datagram network itself is not connection-oriented and does not provide a reliable and orderly complete service. udp datagram network directly, so it's just trying to provide service delivery, packets may be lost during transmission. So why is using the same TCP datagram network, we were able to achieve a reliable connection-oriented transport service it? In fact, because of the reliable TCP transport instead of relying on the underlying network layer is completed, is entirely at the transport layer, finished at the software level, there is a concrete manifestation of acknowledgment is received in the TCP protocol, there retransmission timeout, duplicate confirmation - fast retransmit and other additional steps.

    It is precisely because udp send and receive datagrams compared to more than so many restrictions, so was able to achieve a reliable transport service, these additional steps also brought many problems: the need to set up more than a lot of fields in the header field to complete those steps, the steps do have the time, resources and other expenses. These header fields most important thing is ack, byte sequence number, seq, which is guaranteed the right of retransmission of lost packets basis.

    However, with these can only do this: Send the message, wait for confirmation, continue to send the next packet after receiving the confirmation, the efficiency is very low. And with the sliding window, the communicating parties do not have to send a message, to confirm receipt of this message the next and then send a message, but can continuously send multiple messages, just do not exceed the window size limit; there it is: TCP overhead than big udp, once the network congestion or packet loss will cause the packet retransmission, which retransmission has increased congestion, TCP was to strictly control the transmission rate to prevent network congestion, according to the recipient of the sliding window Win limit the size of a good transmission rate of the sender.

Conclusion is: (1) a continuous sliding window allows the sender to transmit a plurality of packets (2) in accordance with the transmission rate limit opposing receiver window size of the sender, to prevent congestion

(Note: in practice are not necessarily to each other rwnd receive window size determines the transmission rate, network congestion because there is no consideration of congestion control likewise determines the transmission window size cwnd, and finally taking MIN (cwnd, rwnd sending).)

 

III. Examples of process

  For example on a book:

 

B发送的的rwnd就是报文头部Win字段的值。解释个小问题:为什么前面说发送窗口的结束位置是N+size,而不是nextSeq+size?因为[N,nextSeq)这些字节是发送出去但未收到确认的,是随时有可能重发的,因此结束位置要从N算起,到N+size。

Guess you like

Origin www.cnblogs.com/shen-qian/p/12111666.html