[Computer network] TCP connection: three handshake, four waves

[Computer network] TCP three-way handshake

This article is the original article of the CSDN blogger "Little Book Go". It follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/qzcsu/article/details/72861891

TCP establishment

Insert picture description here

At the very beginning, both the client and the server are in the CLOSED state. The client actively opens the connection, and the server passively opens the connection.
SYN: Synchronization flag bit, used to establish a session connection and synchronize the serial number.
ACK: Acknowledgement flag bit. When ACK=1, the acknowledgment number field is valid. When ACK=0, the confirmation number is invalid.
seq: The sequence number of each byte of the byte stream in a certain transmission direction in the TCP communication process. This is used to confirm the order of the sent data. For example, the current sequence number is 1000, 1000 is sent, and the next sequence number is 2000 .
ack: The acknowledgment number made by TCP to the last seq sequence number, used to respond to the TCP segment, adding 1 to the sequence number seq of the received TCP segment.
Client→ Server: SYN=1, initial sequence number seq=x
Server→ Client: SYN=1, ACK=1, initial sequence number seq=y, ack=x+1 (For the server, the data sent starts from the sequence number y starts, the received data starts from the sequence number x+1)
Client→ Server: SYN=1, ACK=1, seq=x+1, ack=y+1 (For the client, the data sent starts from the sequence number Starting from x+1, the received data starts from the serial number y+1)

Why does the TCP client send an acknowledgment at the end?

In a word, it is mainly to prevent the connection request message that has been invalidated from being suddenly transmitted to the server, thereby causing errors.

If you use two handshake to establish a connection, suppose there is a scenario where the client sends the first request connection and it is not lost, just because the time spent in the network node is too long, because the TCP client is late. I did not receive the confirmation message for a long time, thinking that the server did not receive it. At this time, the message was sent to the server again. After that, the client and the server completed the connection after two handshake, transmitted data, and then closed the connection. At this time, the connection request that was stranded before, the network is unblocked and it reaches the server. This report text should be invalid. However, the two handshake mechanism will allow the client and the server to establish a connection again, which will cause unnecessary errors. And waste of resources.

If a three-way handshake is used, even if the invalid message is transmitted, the server receives the invalid message and replies to the confirmation message, but the client will not send the confirmation again. Since the server does not receive the confirmation, it knows that the client did not request a connection.

TCP connection release

Insert picture description here

The client actively closes, and the server passively closes
FIN: The end flag indicates that there is no more data to send, and the connection is about to be closed.
Client→ Server: FIN=1, sequence number seq=u
Server→ Client: ACK=1, sequence number seq=y, ack=u+1
Server→ Client: FIN=1, seq=w, ack=u+1
Client → Server: SYN=1, ACK=1, seq=u+1, ack=w+1

Why is it a three-way handshake to establish a connection, and four waves to close the connection?

When establishing a connection, the server is in the LISTEN state, and after receiving the SYN message for establishing a connection request, it puts the ACK and SYN in one message and sends it to the client.
When the connection is closed, when the server receives the FIN message from the other party, it only means that the other party no longer sends data but can still receive data, and it may not send all the data to the other party, so the server can close it immediately or send it. After some data is sent to the other party, a FIN message is sent to the other party to indicate that they agree to close the connection now. Therefore, one's own ACK and FIN are generally sent separately, which leads to one more time.
————————————————
Copyright Statement: This article is the original article of the CSDN blogger "Little Book Go", and it follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and This statement.
Original link: https://blog.csdn.net/qzcsu/article/details/72861891

Guess you like

Origin blog.csdn.net/Zrackpot/article/details/115302926