TCP transport connection management
Because TCP three handshakes to establish a connection, four waves to release the connection is very important, so attached “Computer Networks (7th edition) – Xie Xiren” book in this chapter detailed description: gitee.com/huihut/inte…
TCP Three-way handshake establishes a connection
Description of the whole process of establishing a TCP connection
The client sends a SYN to the server, indicating that the client requests to establish a connection.
The server receives a SYN from the client and replies with a SYN+ACK (agreeing to establish a connection).
After receiving a SYN+ACK packet from the server, the client replies an ACK packet to the server.
The server receives an ACK from the client. The connection is established and data can be transferred.
Why does TCP have a three-way handshake?
Because the channel is unreliable, and TCP wants to establish reliable transmission over the unreliable channel, then triple communication is the theoretical minimum. (UDP, on the other hand, does not require a three-way handshake because it does not require reliable transport.)
【答案 2 】 because both parties need to confirm that the other party has received the serial number they sent, the confirmation process requires at least three communications.
【答案 3 】 an error is generated in case the invalid connection request segment is suddenly sent to the server.
TCP waves four times to release the connection
[Description of the TCP connection release process]
The client sends the FIN to the server, indicating that the client does not need to send data to the server (request release from the client to the server).
The server receives the FIN sent by the client and replies an ACK to the client (agreeing to release the connection from the client to the server).
The client receives an ACK from the server, at which point the connection from the client to the server is released (but the connection from the server to the client is not released, and the client can still receive data);
The server continues to send unfinished data to the client.
If the server sends a FIN+ACK to the client, the server finishes sending data. (If the server requests to release the connection from the server to the client, the request will be released automatically after a period of time even if the client does not receive a reply.)
The client receives a FIN+ACK from the server and replies with an ACK (agreeing to release the connection from the server to the client).
After receiving the ACK from the client, the server releases the connection from the server to the client.
Why does TCP wave four times?
Question 1: Why does TCP wave four times? / Why does TCP take three times to establish a connection and four times to release a connection?
Answer 1: Because TCP is in full-duplex mode, when the client requests to close the connection, the client closes the connection to the server (wave once or twice), the server continues to transfer the incomplete data to the client (data transfer), and the server closes the connection to the client (wave three or four times). Therefore, when a TCP connection is established, ACK and FIN packets are sent separately (data transmission is interrupted). When a TCP connection is established, ACK packets and SYN packets are sent together (the second handshake). Therefore, it takes three attempts for the TCP connection to be established and four attempts for the TCP connection to be released.
[Q2] Why can ACK and SYN be sent together for TCP connections, but ACK and FIN are sent separately for release? (ACK and FIN separate for second and third wave)
【答案 2 】 when the client requests release, the server may have data to transmit to the client, so the server must first respond to the client’s FIN request (the server sends ACK), and then transmit data. After the transmission is complete, the server submits a FIN request (the server sends the FIN). When connecting, there is no intermediate data transfer, so ACK and SYN can be sent together.
[Q3] Why does client release require time-wait 2MSL?
【答案 3 】
(1) To ensure that the last ACK packet sent by the client can reach the server. If the FIN+ACK packet fails to arrive, the server retransmits the FIN+ACK packet. Then the client retransmits the ACK packet and restarts the timer.
(2) Prevent invalid connection request message segments from appearing in this connection. Time-wait duration 2MSL enables all message segments generated during the duration of the connection to disappear from the network. In this way, old connection message segments will not appear in the next connection.
TCP finite state machine
TCP finite state machine image
This is the end of today’s sharing, you must learn C++ yo ~
For those of you who are ready to learn C/C++ programming, if you want to improve your core programming skills, you might as well start now!
Wechat official account: C language programming Learning base
Organize and share (years of learning source code, project actual combat video, project notes, basic introduction tutorial)
Welcome to change careers and learn programming partners, use more information to learn and grow faster than their own thinking oh!