[TCP protocol] "Three handshakes, four waves" of connection management

Hello, everyone~ I am your old friend: Protect Xiao Zhou ღ  


This issue brings you one of the mechanisms of the TCP transmission control protocol in network programming to ensure reliable data transmission - connection management . To open a connection, how to perform "handshake" and "waving" operations, this article will analyze it for you~~


This issue is included in the blogger's column : JavaEE_Protect Xiao Zhouღ's Blog-CSDN Blog

Suitable for programming beginners, interested friends can subscribe to view other "JavaEE Basics".
Stay tuned for more highlights: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★*'


1. Review of the previous issue

1.1 TCP message structure

 Strive to make a simple analysis of the TCP message:

Serial number and confirmation number: Each occupies 32 binary bits  for reliable data transmission. Each byte of the transmitted byte stream will be numbered in sequence to ensure that the order of data transmission is the same as the order of reception. The sequence number represents the sequence number of the currently sent data packet, and the confirmation number represents the sequence number of the next data packet expected to be received. The significance of the serial number and confirmation number is to ensure that the order of data transmission is the same as the order of reception.           

Flag bits: including ACK, SYN, FIN, RST, PSH, and URG each occupying a binary bit.

It is set to 0 by default, if it is set to 1, it means true.

ACK: used to confirm receipt of data packets;

SYN: Determine whether the communication parties have established a connection;

FIN: used to close the connection; 0

RST: used to reset the connection;

PSH: used to prompt the receiver to push the data to the application immediately instead of entering the receive buffer;

URG: Used to indicate that there is urgent data in the TCP packet.


1.2 Acknowledgment and timeout retransmission

Acknowledgment response:

When the receiver receives the data sent by the sender, it will give the sender a field feedback with a TCP header, where ACK = 1 (a binary bit), indicating that I have received the data. 

For the above mechanism, there are three situations:

1. The data is directly lost during the transmission process, and the receiver cannot give feedback if the data is not received.

2. After receiving the data, the receiver gives feedback to the sender, but the feedback message is lost during transmission.

3. When the sender does not receive feedback for a certain period of time, it will trigger a timeout retransmission, and then the retransmitted data will be lost

Timeout retransmission:

If the sender does not wait for the acknowledgment from the receiver within a certain period of time, it will retransmit the " data packet " . times, TCP will try to re-establish the connection for the communication, if the connection fails, it will terminate this network communication.

How to re-establish the connection and terminate the network communication here is the topic of this article~~

For specific content, you can read the blogger’s previous blog: [TCP Protocol] Message Format, Mechanism for Reliable Data Transmission (1)_Protecting Xiao Zhouღ’s Blog-CSDN Blog


2. Connection management

TCP transport layer protocol, protocol function: to ensure the reliability of data transmission between the two parties in communication.

The important factor of guarantee is the mechanism of acknowledgment response and timeout retransmission. The receiver will give feedback to the sender after receiving the data. This feedback is a field with a TCP fixed header. The TCP header contains flag bit information, and each flag bit only Occupies a binary bit, so according to the flag bit of the TCP header, we can judge a state of the current " TCP packet "

The connection between the two parties in the TCP protocol communication is established through a three-way handshake.


2.1 Three-way handshake

Handshake (handshake) refers to the communication between the two parties, the network interaction, between the sender and the receiver, through three interactions, a connection relationship is established. The connection relationship refers to: the two parties in the communication have recorded each other's information~

Why mention the client and the server, because in our daily communication, the two parties in the communication do not directly establish a connection, but first establish a connection with the server, and the information is "transferred" through the server.

The "will" to establish a connection must be initiated by the client

  1. The client sends a SYN packet to the server , indicating a request to establish a connection.

  2. After receiving the SYN packet, the server replies with a SYN+ACK packet, indicating that it agrees to establish a connection.

  3. After the client receives the SYN+ACK packet, it replies with an ACK packet, indicating that the connection is established successfully.

SYN: (a binary bit) set to 1 means that one party wants to apply for a connection to the other party. Indicates that the current TCP datagram is a synchronous message.

ACK: a binary bit), setting 1 means to give feedback and confirm receipt of data. Indicates that the current TCP datagram is a reply message.

Schematic diagram of establishing a connection between the two communicating parties :

The blogger used it here, and simulated the scene of making a call to "establish a connection". Maybe there is a certain period of time before such a call, but the main purpose is to verify whether the sending and receiving capabilities of the two communicating parties are normal . The normality of the interaction process is confirmed, which is also the basis for subsequent reliable data transmission.

Why is there a three-way handshake, can it be done twice, can it be done four times?

First of all, two handshakes can only confirm that the sending and receiving function of one party is normal.

For multiple handshakes, splitting the AYN and ACK in the middle and sending them separately can also achieve the purpose of verification, but it is completely unnecessary, because a TCP data segment needs to be encapsulated and divided layer by layer (TCP / IP Five-layer communication model), sending in two times, the efficiency is very low.


2.3. Four waves

Waving four times means that the two parties in communication are disconnected, which is also a nice name.

The "will" to disconnect may be initiated by both the client and the server.

  1. The client sends a FIN packet to the server , indicating that it wants to disconnect.

  2. After receiving the FIN packet, the server replies with an ACK packet , indicating that the disconnection request has been received.

  3. The server then sends a FIN packet , agreeing to disconnect.

  4. After the client receives the FIN packet, it replies with an ACK packet , indicating that the disconnection request has been received.

FIN: (a binary bit) set to 1 means that one party wants to apply to the other party for disconnection . Indicates that the current TCP datagram is an end message.

Schematic diagram of disconnection between the two communicating parties :

Through the above process, the disconnection is that the two communicating parties each send a FIN (end message) to the other party, and then each send an ACK (response message) to this.

After learning the "three-way handshake", we will find that there is an operation SYN + ACK, request connection + confirmation response, and the two flag bits can be fed back in the same TCP datagram, so there are three


But in disconnection, FIN and ACK are transmitted separately, so disconnection requires four .

The reason is that SYN + ACK are both completed by the operating system kernel and can be triggered at the same time.

TCP belongs to the transport layer protocol. As a program developer, the focus is actually on the development of the application layer, but they choose to use the Socket interface (API) based on the TCP protocol for development. Transport layer-network layer-data link layer-physical layer, these 4 layers have actually been encapsulated by the operating system, and they are called layer by layer, so the application layer directly uses the interface provided by the transport layer protocol, so there is no need to pay too much attention other internal implementations.

Wave four times:

ACK and FIN are triggered by different machines, and ACK is completed by the system kernel, so after receiving the FIN-disconnection request, the sender will be given a confirmation response at the first time.

FIN - Disconnection is controlled by the application code. In Java network programming, FIN is triggered when the close() method in Socket (interface) is calledsend a TCP datagram and FIN is set to 1 the end message.

For example: the server finds that the client has sent an end message, so it calls the close() method by itself, thus triggering the second end message. Of course, what to use to call the close() method depends on how the program is written.


About the situation that the program does not call close() to trigger the end message

For example: in the program on the client side, no close() is written at all. In this case, the server sends an end request FIN to the client——the server cannot receive the end message fed back by the customer service end. Is it impossible to disconnect the connection, no no no

If the client's program ends - the process ends - the application ends - such as qq ends running, it will automatically trigger "close()", and the resource will end with the end of the process, and the system will control the recovery, so it will trigger the FIN end report arts. At this point, the client's qq application is running, but the TCP connection is still there (maintained by the operating system kernel), until it finishes waving four times with the qq server~and vice versa.

It should be noted that the TCP protocol is full-duplex, that is, the client and server can send and receive data at the same time. Therefore, when disconnecting, you need to wave four times, and the operating system kernel maintains the wave process to ensure that both parties have disconnected.


Well, here,  the blogger  of the "three-way handshake and four-way wave" of [TCP protocol] connection management  in network programming has finished sharing, I hope it can be helpful to everyone, and welcome criticism and correction if there is anything wrong.

Thank you to everyone who read this article, and more exciting events are coming: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★* 

When I met you, all the stars fell on my head ...

Guess you like

Origin blog.csdn.net/weixin_67603503/article/details/130354669