1. TCP is a connection-based protocol
Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream based transport layer communication Protocol. The so-called connection-oriented refers to the establishment of a complete communication channel between the two parties before the communication, and this channel is the connection.
TCP is a connection-based protocol, that is, before sending or receiving data, a reliable connection must be established with the peer. Three handshakes are required to establish a TCP connection, and four handshakes are required to release the connection.
2. Establish the connection
The process of establishing a connection generally requires three handshakes, as shown below:
Before the handshake, the client that actively opens the connection ends the CLOSE phase, and the server that passively opens the connection also ends the CLOSE phase and enters the LISTEN phase. Then enter the three-way handshake:
(1) First the client sends a SYN packet to the server and waits for the server to confirm it
- Sign bit is
SYN
To request to establish a connection - The serial number for
Seq = x
(x
As a general rule, be1
) - Then the client enters
SYN-SENT
phase
(2) After receiving the SYN packet from the client, the server confirms the PACKET, ends the LISTEN phase, and returns a TCP packet
- Sign bit is
SYN
andACK
Indicates the client confirmation packetSeq
The serial number is valid. The server can normally receive the data sent by the client and agrees to create a new connection - The serial number for
Seq = y
- Confirmation number is
Ack = x + 1
Is the serial number of the received clientSeq
And add its value1
As your own confirmation numberAck
, and then the server side entersSYN-RECV
phase
(3) After the client receives the SYN + ACK packet, it is clear that the data transmission from the client to the server is normal, thus ending the SYN-sent phase. And returns the last packet
- Sign bit is
ACK
“, confirming that the server receives a signal agreeing to connect - The serial number for
Seq = x + 1
, indicating that the confirmation number of the server is receivedAck
And uses its value as its own ordinal value - Confirmation number is
Ack= y + 1
Indicates that the serial number of the server is receivedseq
And add its value1
As your own confirmation numberAck
The value of the - Then the client enters
ESTABLISHED
After receiving a packet from the client confirming the receipt of server data, the server knows that the data transmission from the server to the client is normal. In this way, the SYN-RECv phase is ended and the ESTABLISHED phase is entered. The three-way handshake is complete.
Why the “three” handshake?
Because three times is the minimum number of times that both the client and server can send and receive data:
- Client > Server: The client has the sending capability
- Server > Client: The server has the receiving and sending capabilities
- Client > Server: The client has the receiving capability
2. Release the connection
The process of releasing the connection generally requires four waves of the hand, as shown below:
This assumes that the client actively releases the connection. The client that actively releases the connection before waving ends the ESTABLISHED phase, followed by four waves:
(1) The client sends a TCP packet to the server to indicate that it wants to release the TCP connection
- Labeling for
FIN
Said,Request to release connection - The serial number for
Seq = u
- Then the client enters
FIN-WAIT-1
Phase, i.e.,Half shut down stageAnd stop sending communication data to the server
(2) After receiving the FIN packet from the client, the SERVER ends the ESTABLISHED phase, enters the close-wait phase, and returns a TCP packet
- Labeling for
ACK
Said,Description The client received the request to release the connection - The serial number for
Seq = v
- Confirmation number is
Ack = u + 1
Is added to the serial number of the received client packet1
As the confirmation number of this packetAck
The value of the - The server then prepares to release the connection from the server side to the client side
After receiving the TCP packet from the server, the client confirms that the server has received the request to release the connection from the client and enters fin-WaIT-2 phase.
(3) After the server sends an ACK message, the server sends the remaining data to the client. After the transmission is completed, the server is ready to release the connection from the server to the client and sends a TCP message to the client
- Labeling for
FIN
andACK
Said,Ready to release the connection - The serial number for
Seq = w
- Confirmation no.
Ack = u + 1
, indicates that the serial number of the received client packet is changedSeq
The value of the add1
As the confirmation number of this packetAck
The value of the
Then the server ends the close-wait phase, enters the last-ACK phase, and stops sending data to the client.
(4) The client receives a TCP packet from the server to confirm that the server is ready to release the connection. Then the client enters the time-wait phase and sends a packet to the server
-
The flag bit is ACK, indicating that a signal has been received that the server is ready to release the connection
-
The sequence number is Seq= U + 1, indicating that the Ack number of the received packet is used as the sequence number of this section
-
The acknowledgement number is Ack= W + 1, which indicates that Seq is used as the acknowledgement number of this packet
The client then begins to WAIT for 2 MSL in the time-wait phase. After receiving the TCP packet from the client, the server enters the CLOSED phase, which officially confirms that the connection between the server and the client is CLOSED. After the client waits for 2 MSL, it enters the CLOSED phase, which completes four waves.
Why “four” waves?
Because TCP is a full-duplex protocol, each channel must be disabled separately, and the receiving and sending in the two directions must be disabled separately.
Reference:
“LeetBook”