当前位置:网站首页>TCP triple handshake

TCP triple handshake

2022-06-24 13:19:00 Software up

Preface

TCP When the protocol passed down data , client (Client) And the service side (Server) Will establish a connection , Then segment the files to be transferred , And provide reliable transmission and flow control ! After the data transfer is complete , The current session should also be disconnected , Avoid waste of resources . all TCP The third handshake is the process of establishing a connection , And four waves are the process of disconnecting !

TCP Basic knowledge

Transmission control protocol ( English :Transmission Control Protocol, abbreviation :TCP) It's a connection oriented 、 reliable 、 Transport layer communication protocol based on byte stream

TCP Header format

  • Serial number : The random number generated by the computer when establishing the connection is used as the initial value , adopt SYN The packet is sent to the receiving host , Every time it's sent , The size of the number of bytes is accumulated once . It is used to solve the network packet disorder problem
  • Confirm answer signal : Next time [ expect ] The serial number of the data received , After receiving the acknowledgement, the sender can consider that the data before the serial number has been received normally . To solve the problem of no packet loss
  • Control bits

ACK: This bit is 1 when , Confirm that the reply field becomes valid ,TCP Except when the connection is initially established SYN This bit outside the package must be set to bit 1

RST: This bit is 1 when , Express TCP When an exception occurs during connection, the connection must be forcibly disconnected

SYN: This bit is 1 when , Indicates that you want to establish a connection , And set the initial value of the serial number in the field of the serial number

FIN: This bit is 1 when , No more data will be sent in the future , Want to disconnect . When communication ends and you want to disconnect , The hosts of both sides can exchange with each other FIN Position as 1 Of TCP paragraph .

Three handshakes

  • The first handshake : When establishing a connection , client A send out SYN package (SEQ_NUMBER=x) To the server B, And enter SYN_SEND state , Waiting for server B confirm .
  • The second handshake : The server B received SYN package , Customer must be confirmed A Of SYN(ACK_NUMBER=x+1), At the same time, I also send a SYN package (SEQ_NUMBER=y), namely SYN+ACK package , At this time, the server B Get into SYN_RECV state .
  • The third handshake : client A Receive server B Of SYN+ACK package , To the server B Send confirmation package ACK(ACK_NUMBER=y+1), This package has been sent , client A And the server B Get into ESTABLISHED state , Complete three handshakes .
  • Complete three handshakes , Client and server begin to transmit data .

Four waves

  • First wave : The client intends to close the connection , A TCP The first one FIN The flag bit is set to 1, Its serial number is the sequence number of the last byte of the previously transmitted data plus 1(seq=x)
  • Second wave : After the client receives the message , Send it to the client ACK Reply message ,ACK=1,ack=x+1, And bring your own serial number seq=y, here , The server enters CLOSE-WAIT( Turn off waiting ) state .
  • After the client receives the confirmation request from the server , here , The client enters FIN-WAIT-2( Stop waiting 2) state , Wait for the server to send the connection release message ( Before that, you need to accept the last data sent by the server ).
  • Third wave : After the server sends the last data , Send the connection release message to the client ,FIN=1,ack=x+1, Because it's half closed , The server probably sent some more data , Suppose the serial number at this time is seq=z, here , The server is in LAST-ACK( Final confirmation ) state , Wait for the client to confirm .
  • Fourth wave : After the client receives the connection release message from the server , Confirmation must be sent ,ACK=1,ack=z+1, And my serial number is seq=h, here , The client enters TIME-WAIT( Time waits ) state . Note that this time TCP The connection has not been released , After the longest message segment life , When the client cancels the corresponding TCB after , Only enter CLOSED state .
  • As long as the server receives the confirmation from the client , Enter immediately CLOSED state . Again , revoke TCB after , It's the end of this TCP Connect . You can see , End of server TCP The connection time is earlier than the client .

Why not use two handshakes to connect ?

answer : Three handshakes complete two important functions , Both sides should be ready to send data , Also know that both sides know that each other is ready ! If you change to two handshakes, it will lead to deadlock . Suppose the client sends a connection request to the server , The server received this packet , And sent a confirmation response packet . According to the agreement of two handshakes , The server thinks that the connection has been successfully established , You can start sending data packets . But , When the response signal of the server is lost during transmission , The client does not know whether the server is ready , I don't know what serial number the server will create , The client even doubts whether the server has received its own connection request packet . under these circumstances , The client thinks that the connection has not been established successfully , Any data packets sent by the server will be ignored , Only wait for the connection to confirm the reply group . The server sends out packets after timeout , Send the same packet over and over again . This creates a deadlock .

The second of the four waves 、 What happens when you merge three times ?

answer : Because when the server receives a request from the client to disconnect , There may still be some data not sent out , Then reply first ACK, Indicates that a disconnect request has been received . Wait until the data is sent FIN, Disconnect the data transfer from the server to the client .

Only when all the messages on the server end are sent , To send FIN message , So we can't send . So it takes four waves .

原网站

版权声明
本文为[Software up]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210525165027062L.html