TCP/IP three-way handshake and four-way handshake

Source: Internet
Author: User

Three-way handshake to establish a connection:

First handshake:The client sends the SYN Packet (SEQ = x) to the server and enters the syn_send status. wait for confirmation from the server;

The second handshake:When the server receives the SYN packet, it must confirm the customer's Syn (ACK = x + 1) and send a SYN Packet (SEQ = Y), that is, the SYN + ACK packet, the server enters the syn_recv status;

The third handshake:The client receives the server's SYN + ACK packet and sends the ACK (ACK = Y + 1) Confirmation packet to the server. After the packet is sent, the client and server enter the established status and complete three handshakes.

The package transmitted during the handshake does not contain data. After the three-way handshake is completed, the client and the server formally start to transmit data. Ideally, once a TCP connection is established, the TCP connection will remain until either of the two parties closes the connection.

 

Data transmission process: 

A. Timeout retransmission

The timeout retransmission mechanism is used to ensure the reliability of TCP transmission. Each time a data packet is sent, the sent data packet has a seq number. After receiving the data, the receiving end replies to ack for confirmation, indicating that the data of a certain seq number has been received. After sending a seq packet, the sender waits for a period of time. If the corresponding ack reply is not received, the sender considers the packet to be lost and retransmits the packet.

B. Fast retransmission

The recipient finds that some data packets are lost. An ACK packet is sent to the sender to send the lost packet. If the sender continuously receives an ACK packet with the same label, the fast retransmission of the client is triggered. Compared with timeout retransmission and fast retransmission, it can be found that the sender times out while the sender triggers retransmission. The fast retransmission means that the receiver actively tells the sender that the data has not been received, then trigger the sender retransmission.

C. Traffic Control

TCP Sliding Window traffic control. There is a field in the TCP Header called Window, also called advertised-window. This field is used by the receiving end to tell the sending end how many buffers can receive data. Therefore, the sender can send data based on the processing capability of the receiver, without causing the receiver to be unable to process the data. Sliding windows can be a mechanism to improve TCP transmission efficiency.

D. Congestion Control

The sliding window is used for traffic control. Traffic control only focuses on the status of the sender and receiver, rather than the communication of the entire network. Congestion Control is based on the entire network. Consider this scenario: when the network latency increases suddenly at a certain time point, TCP will only respond to this issue by re-transmitting data. However, re-transmission will cause a heavier network burden, as a result, it will lead to a greater delay and more packet loss. Therefore, this situation will go into a vicious circle and be magnified. Imagine if tens of thousands of TCP connections in a network do this, a "network storm" will be formed immediately, and the TCP protocol will drag the entire network. Therefore, TCP introduces a congestion control policy. Congestion policy algorithms include slow start, congestion avoidance, congestion occurrence, and rapid recovery.

 

Disconnect after four handshakes: 

The first wave:The active closing party sends a fin to disable the data transmission from the active party to the passive closing party, that is, the active closing party tells the passive closing party that I will not send data to you (of course, if the data sent out before the fin package does not receive the corresponding ack confirmation message, the active closing party will still resend the data), but the active closing party can accept the data at this time.

Second wave:After the passive shutdown party receives the FIN packet, it sends an ACK to the other party and confirms that the serial number is the received serial number + 1 (same as SYN, one fin occupies one serial number ).

The third wave:The passive closing party sends a fin to disable the data transmission from the passive closing party to the active closing party, that is, to inform the active closing party that my data has been sent, no more data will be sent to you.

The fourth wave:After the active closing party receives the fin, it sends an ACK to the passive closing party and confirms that the serial number is received by the serial number + 1. At this point, the last four waves are completed.

 

Two problems:

(1) Why is the three-way handshake and the four-way handshake when the connection is disconnected?

TCP establishes a connection to carry out three handshakes, and disconnect to carry out four times, this is caused by TCP semi-closed, because the TCP connection is full duplex (
That is to say, data can be transmitted in both directions at the same time. Therefore, when you close the service, close the service in each direction separately. If you close the service in one direction, close the service in half.
After one party completes its data transmission, it sends a fin to notify the other party that it will terminate the connection in this direction. When one end receives a fin, it must
The TCP connection at the application layer has terminated data transmission in this direction. Sending fin is usually the result of the application layer disabling.

(2) Why can't I connect two handshakes but three handshakes?

The three handshakes are used to complete two important functions. Both parties must prepare for sending data (both parties know that each other is ready) and allow both parties to negotiate on the initial serial number, the serial number is sent and confirmed during the handshake.
Now, only two handshakes are needed to change the three-way handshake. A deadlock may occur. For example, considering the communication between computer S and C, assuming C sends a connection request group to S, s receives the group and sends a confirmation Response Group. According to the two handshakes, s considers that the connection has been successfully established and can start sending data groups. However, if C's Response Group in S is lost during transmission, it will not know if S is ready or what serial number S has created, c even doubts whether s has received its own connection request group. In this case, C considers that the connection has not been established successfully, and ignores any data groups sent by S, only waiting for the connection to confirm the response group. When the Group sent by S times out, the same group is repeatedly sent. In this way, a deadlock occurs. Therefore, the three-way handshake is used to prevent the invalid Connection Request Message segment from being suddenly transmitted to the host.

 

 

Reference: graphic TCP/IP

Http://blog.csdn.net/qq276592716/article/details/19762121

TCP/IP three-way handshake and four-way handshake

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.