[Learn JAVA from scratch | Article 44] Handshake and wave in TCP protocol

Foreword:

As an important protocol in computer networks, TCP (Transmission Control Protocol) plays a role in ensuring reliable data transmission. In the TCP communication process, the handshake and wave issues are key links that cannot be ignored. Handshake means that when a connection is established, the client and server mutually confirm each other's identity and synchronize parameters to ensure that both parties are ready; while waving means that when the connection is terminated, the two parties inform each other of their wishes to ensure the safe closure of the connection. This article will deeply explore the details and functions of handshake and hand-waving in the TCP protocol, in order to better understand and apply the TCP protocol. 

Handshake in TCP:

TCP's three-way handshake is the process of establishing a TCP connection to ensure that the communication between the client and the server can be carried out reliably.

  1. First handshake (SYN):
    The client sends a SYN packet (synchronization request) to the server, containing a random initial sequence number. This packet tells the server that the client wants to establish a connection and waits for confirmation from the server.

  2. The second handshake (SYN-ACK):
    After the server receives the SYN packet from the client, it will send a SYN-ACK packet (synchronization-confirmation). This packet not only confirms the client's request (ACK), but also sends a random initial sequence number to the client. The server tells the client that the server is willing to accept the connection request and waits for the client's confirmation.

  3. The third handshake (ACK):
    After receiving the SYN-ACK packet from the server, the client sends an ACK packet (confirmation) to the server. The confirmation number in this packet is the initial sequence number sent by the server plus 1, indicating that the client has received the server's response. The client acknowledges the server's connection request and tells the server that data transfer can begin.

After completing the three-way handshake, the TCP connection is successfully established, and the two parties can start reliable data transmission. This three-way handshake process ensures that both parties can confirm the existence of the other party, synchronize the serial number, and establish a reliable connection.

Wave in TCP:

  1. First Wave (FIN):
    When the client decides to close the connection, it sends a FIN packet (terminate request) to the server. The client no longer sends data, but can still receive data sent by the server.

  2. The second wave (ACK):
    After receiving the FIN packet from the client, the server sends an ACK packet (confirmation). At this time, the server enters a half-closed state, that is, it can only send data, but can no longer receive data.

  3. Third wave (FIN):
    After the server has sent all the data, it will send a FIN packet (terminate request) to the client. The server tells the client that it is going to close the connection and send no more data.

  4. The fourth wave (ACK):
    After receiving the FIN packet from the server, the client sends an ACK packet (confirmation). The client tells the server that it has received a terminate request and confirms that it can close the connection.

After four handshakes, the TCP connection is cleanly closed. This process ensures that both parties have a chance to send and receive final data, and eventually close the connection properly. This ensures data integrity and prevents data loss or confusion. Four waves are the safe termination process of a TCP connection.

Common exceptions:

  1. Connection timeout:
    After the client sends a SYN packet, if it does not receive a response (SYN-ACK packet) from the server within a certain period of time, a connection timeout exception may be triggered. This could be due to reasons such as network latency, high server load, or network failure.

  2. SYN flood attack:
    SYN flood attack is a kind of network attack. The attacker sends a large number of forged SYN packets to the server, causing the server to run out of resources and fail to respond to normal user connection requests. This can cause the normal handshaking process to be blocked or delayed.

  3. Semi-connection queue overflow:
    When the server is under high load, if the connection request sent by the client exceeds the capacity that the server can handle, it may cause the semi-connection queue to overflow. This causes the server to fail to accept new connection requests, causing the handshake to fail.

  4. Abnormal close: An abnormal close
    occurs when one party closes or terminates the connection abruptly during a handshake or wave while the other party still expects to continue communicating. This may cause one party of the connection to be unable to complete the handshake process normally, and a timeout mechanism is required to handle abnormal shutdown.

  5. Repeated ACK or FIN packets:
    During the handshake or hand-waving process, if repeated ACK or FIN packets are received, the status of the two parties in the connection may be inconsistent, so that the connection cannot be closed normally or re-established.

These abnormal conditions may have an impact on the establishment and closing of the TCP connection. Appropriate processing and fault tolerance mechanisms are required in the application to ensure the reliability and stability of network communication.

Summarize:

In the TCP protocol, handshaking and waving are important processes to ensure a reliable connection. The handshake ensures that both the client and the server are ready for data transfer through three interactions. And waving through four interactions safely closes the connection to ensure data integrity. However, the handshaking and waving process may encounter exceptions such as connection timeouts, flooding attacks, etc. These exceptions need to be properly handled by the application program to ensure the reliability of network communication. Understanding the principles and common exceptions of TCP handshake and wave will help optimize network communication and ensure the stability and security of data transmission.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

 

Guess you like

Origin blog.csdn.net/fckbb/article/details/132197903