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 isSYNTo request to establish a connection
  • The serial number forSeq = x(xAs a general rule, be1)
  • Then the client entersSYN-SENTphase

(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 isSYNandACKIndicates the client confirmation packetSeqThe 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 forSeq = y
  • Confirmation number isAck = x + 1Is the serial number of the received clientSeqAnd add its value1As your own confirmation numberAck, and then the server side entersSYN-RECVphase

(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 isACK“, confirming that the server receives a signal agreeing to connect
  • The serial number forSeq = x + 1, indicating that the confirmation number of the server is receivedAckAnd uses its value as its own ordinal value
  • Confirmation number isAck= y + 1Indicates that the serial number of the server is receivedseqAnd add its value1As your own confirmation numberAckThe value of the
  • Then the client entersESTABLISHED

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 forFINSaid,Request to release connection
  • The serial number forSeq = u
  • Then the client entersFIN-WAIT-1Phase, 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 forACKSaid,Description The client received the request to release the connection
  • The serial number forSeq = v
  • Confirmation number isAck = u + 1Is added to the serial number of the received client packet1As the confirmation number of this packetAckThe 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 forFINandACKSaid,Ready to release the connection
  • The serial number forSeq = w
  • Confirmation no.Ack = u + 1, indicates that the serial number of the received client packet is changedSeqThe value of the add1As the confirmation number of this packetAckThe 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”