TCP's three handshake and four wave and answer

The three-way handshake of TCP is used to establish a connection.When
establishing a TCP connection, the client and server need to send a total of 3 packets to confirm the establishment of the connection.In
socket programming, this process is executed by the client to trigger
Insert picture description here
the first handshake : The client sets SYN to 1 and randomly generates a value seq = j and sends the data packet to the server. The client enters the SYN_SENT state and waits for the server to confirm the
second handshake : After the server receives the packet, SYN = 1 knows that the client requests to establish a connection. ACK is set to 1 ack = j + 1 Randomly generate a value seq = k and send the packet to the client to confirm the connection request. The server enters the SYN_RCVD state. The
third handshake : After the client receives the confirmation, check whether the ack is +1 ack is 1. If it is correct then the ack is set to 1 ack = k + 1 and the data packet to Server Server to check the proper connection is established Client and Server enters eSTABLISHED state to complete the three-way handshake then begin transmitting data between Client and Server
Insert picture description here
Why It requires three-way handshake twice or four times or five times you can not do it?
---- analysis assuming a special customer requests to establish a connection to the server SYN packet waiting After the server confirms that the server receives the confirmation, if it is a two-way handshake, it is assumed that the server sends data to the client during the second handshake. Retransmission will be performed. Assume that each time the data sent has been lost, the client has been SYN server, it will generate multiple invalid connections and occupy resources. At this time, the server may hang. This phenomenon is the flood attack of SYN.
---- The third handshake is to prevent the client from abandoning the connection and restarting a connection request if the client fails to receive the server confirmation message, but the problem is that the server does not know that the client has not received it, so he will receive two Connections waste connection costs if this is the case each time it will waste multiple connection costs

The four wave of TCP is mainly
Insert picture description here
the first wave when the TCP port is disconnected : Host 1 (which can be a client or a server) sends a FIN segment to Host 2 At this time, Host 1 enters the FIN_WAIT_1 state to indicate the host 1 There is no data to send to host 2.
** The second wave: ** Host 2 received the FIN segment sent by host 1 and returned an ACK segment to host 1. Host 1 entered the FIN_WAIT_2 state. Host 2 told host 1 I also have no data to send. I can close the connection. The
third wave: the host 2 sends a FIN segment to the host 1 to close the connection and the host 2 enters the CLOSE_WAIT state. The
fourth wave: the host 1 receives the host 2 The FIN segment sent to the host 2 sends an ACK segment to the host 2 Random host 1 enters the TIME_WAIT state Host 2 receives the ACK segment from host 1 and closes the connection at this time host 1 waits for 2MSL and still has not received a reply to prove Server-side has been closed properly then the host 1 can also close the connection
Insert picture description here
Why TIME_WAIT state need to go through 2MSL (maximum segment lifetime) CLOSE to return to the state?
---- first MSL is the maximum packet lifetime is either The presence of packets that exceed the maximum time period the packet is discarded on the network
---- a TIME_WAIT TCP wait 2MSL initiated when an end of the TCP ACK packet is transmitted three times the active closed fourth wave after wave after the completion of entering The main purpose of this state to wait for 2MSL time is to prevent the last ACK packet from being received by the other party.The other party will resend the third waved FIN packet after timeout.The terminal will actively close the terminal and receive another ACK packet after receiving the retransmitted FIN packet. When the packet is in the TIME_WAIT state, the ports on both ends cannot be used. Wait until the end of the 2MSL time before continuing to use. When the connection is in the 2MSL waiting phase, any late segments will be discarded

The client enters the time_wait state after sending the last ack, but how does it know if the server has received the ack?
The ack packet sent by the actively closed party due to network reasons is likely to be delayed, which triggers the passive connection party to retransmit the FIN packet. One go and one go is twice the MSL duration. If the actively closed party skips TIME_WAIT and directly enters the closed or stays in time_wait for less than twice the MSL, then when the delayed packet sent by the passively closed party arrives earlier, it may appear similar to the following The problem: the old tcp connection no longer exists. The system can only return the rst packet at this time. The new tcp connection is established. The delay packet may interfere with the new connection. This is why you need to wait for 2msl.

Why is the three-way handshake when the connection is closed and the four-way wave?
Because when the server receives the syn connection request message from the client, it can directly send a syn + ack message. The ack message is the syn message used to respond. It is used for synchronization, but when the connection is closed, when the server receives the fin message, it is very likely that the socket will not be closed immediately, so I can only reply to an ack message to tell the client "I received the message you sent". Only wait for me After all the messages on the server are sent, I can send the fin message because I ca n’t send them together, so I need to wave four times.

Published 41 original articles · Likes2 · Visits 1836

Guess you like

Origin blog.csdn.net/weixin_43883485/article/details/105389739