当前位置:网站首页>The illustration shows three handshakes and four waves. Xiaobai can understand them

The illustration shows three handshakes and four waves. Xiaobai can understand them

2022-06-23 06:58:00 Shu Yang

        TCP Provisions of the agreement , A connection must be established before transmitting data ( Three handshakes ), Release the connection after data transfer ( Four waves ).

       TCP The header of the message segment is shown in the following figure , Before introducing three handshakes and four waves , Need to know TCP Some fields in the header of the message segment , Because Xiaobai can understand the series , Just a few things you must know :

  • seq Serial number :seq As a sender , The... Of the data sent 1 Number of bytes .

  • ack Confirmation no. :ack As a receiver , The... Of the next message segment expected to be received 1 Number of bytes .
            What do the above two fields mean , for instance ,A to B Send a paragraph TCP message , In this message seq=100, Express A towards B The data sent is 1 The sequence number of bytes is 100, If the data length of this message is 200 Bytes , that B After receiving this message correctly , Want to A Return a message , In this message ack=100+200+1=301, Express B The sequence number of the first byte of the next message segment expected to be received is 301.
            If B Reply message 2 in ack<301, The assumption is 201, This indicates that from the serial number 201~300 The data of B None of them received ! Now A You know that you need to resend the lost data , This is it. TCP One of the ways to ensure reliable transmission .
            Then you must have questions , A message contains both seq And then there is ack, Is it the sender or the receiver ? answer :TCP It's full duplex , Can send and receive at the same time , Both sides of the communication can act as the sender and the receiver , therefore ,A In the B The message that sends the data also carries the reply B Of ack, That is to say seq and ack It works at the same time . Of course , It can also be as mentioned above , One is the sender and the other is the receiver , Just make one of the two fields valid .

  • SYN Sync bit :SYN=1 It means that this is a message requesting or accepting connection .

  • ACK Confirmation bit : Only if the confirmation bit ACK=1 The confirmation number ack It works , When ACK=0 when ack Invalid .
            I said before. SYN=1 It means that this is a message requesting or accepting connection . When SYN=1 And ACK=0 It indicates that this is a message requesting connection , When SYN=1 And ACK=1 It means that this is a message that accepts the connection , Don't ask why , Under the agreement .

  • FIN Stop bit : When FIN=1, Indicates that the sender's data of this message segment has been sent , Request to release connection .

       

       

Three handshakes

        For simplicity , The picture only shows TCP Part of the premiere .
        The first handshake :

       
        The second handshake :

       
        The third handshake :

        The above figure is the following classic figure :

       

       

Why three handshakes , Not twice ?

Answer one :
        If this happens now :A The first connection request message sent , But this connection request message has been stuck in the network for a long time , The delay is long until the connection is released B. This was originally a message that had long failed , but B I don't know ,B I mistakenly thought A Another connection request was made , So I happily went to A A confirmation message was replied , Agree to establish a connection .
        The problem arises , If we now use two handshakes , So the above situation ,B The connection has been established after the reply . and A It doesn't have to be the same B Establishing a connection , Of course, they will not pay attention to B Confirmation message sent , Not to mention B send data .B I don't know these ,B Think the connection has been established , Waiting in silence A Send data , This is a waste B Resources for .
        So a third handshake is necessary .
       
Answer two :
        You can also think like this : After the first handshake ,B got it —— I can receive A Message sent ( I can take it ). After the second handshake ,A got it —— I sent messages from the past B To receive , And I can also receive B Message sent ( I can send and receive ). After the third handshake ,B got it —— I sent it to A The news of A To receive ( I can send ). After three handshakes , Communication forms a closed loop , Both sides know that they can send and receive , It's ready to transfer data .
        If there is no third handshake ,B In fact, I don't know whether I can successfully send messages , because A I didn't reply to the message I sent , You may not be able to send messages successfully , So this third handshake is needed .

       

       

TCP Can I carry data during connection establishment , Why? ?

        answer : The message segments of the first handshake and the second handshake cannot carry data , The message segment of the third handshake can carry data . Why? ?TCP Under the agreement .

       

       

Four waves

        TCP It's full duplex , Both sides of communication can send and receive data , We can take this as an example , Sending data means , Receiving data is listening , To release the connection completely , Both sides should not send ( say ) Not accept ( listen ).
       
        First wave :A No more sending data .

       
        Second wave :B No longer receiving data .
        here ,TCP Connection entered Half closed , namely A There is no data to send ,B I won't accept it , but B You can still send data ,A Can still receive data .

       
        In the half closed state B Possible direction A And sent a lot of data , When B When there is no data to send , Suppose the current serial number has arrived w, namely seq=w, At this time B Send connection release message ,B The confirmation number sent last time must be repeated ack=u+1.

        Third wave ,B No more sending data .

       
        A Upon receipt of B After the connection release message of , This must be confirmed , After sending the confirmation message ,A Will enter the TIME-WAIT( Time waiting state ). Please note that ,A After sending the confirmation message TCP The connection was not immediately released , It has to take time for the timer (TIME-WAIT timer) Time set 2MSL(2 Maximum message segment life ) after ,A To get into CLOSED state .

        Fourth wave ,A No longer receiving data .

       
        The above figure is the following classic figure :

       

       

Why? A stay TIME-WAIT The state must wait 2MSL Time for ?

        First of all , In order to ensure A The last one sent ACK Message segments can reach B.
        second , Prevent the invalid connection request message segment from appearing in this connection .

       

       
Reference books 《 computer network 》 Edited by Xie Xiren .

原网站

版权声明
本文为[Shu Yang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230556565495.html