TCP three-way handshake and four-way disconnection
TCP is a connection-oriented service. The connection-oriented service is the abstraction of the telephone system service mode.
Establish a connection, data transmission, and terminate the connection. The TCP connection is called a three-way handshake. Let's take a look at the three-way handshake.
TCP three-way handshake process
1. host a sends a data segment containing the flag of the synchronous serial number to host B and requests a connection from host B,
Host a tells host B two things: I want to communicate with you; which serial number can you use as the starting data segment to respond to me.
2. After receiving the request from host a, host B responds to host a with a data segment with the ACK and SYN signs, and also tells host a two things:
I have received your request. You can transmit data. Which serial number do you want to use as the initial data segment to respond to me?
3. When host a receives the data segment, it sends a confirmation response to confirm that it has received the data segment of host B: "I have received a reply. Now I want to transmit the actual data.
In this way, three handshakes are completed, and data can be transmitted between host a and host B.
Features of three handshakes
No data at the application layer
The SYN flag is set to 1 only when a TCP production connection is established.
The SYN flag is set to 0 after the handshake is complete.
4 times disconnected
1 When host a completes data transmission, set the control bit fin to 1 and request to stop the TCP Connection
2. Host B responds to fin after receiving it, confirming that the TCP connection to this party will be closed, and the ACK will be set to 1.
3. the B end then requests a reverse shutdown request to set fin to 1.
4. host a confirms the request of host B, sets Ack to 1, and closes the request in both directions.
It can be seen from the three-way handshake and four-way disconnection of TCP that TCP uses connection-oriented communication mode, which greatly improves the reliability of data communication and enables the sending data end
It interacts with the acceptor before the formal data transmission, laying a reliable foundation for the formal data transmission.
Glossary
One of the control bits of the ack tcp Header, which confirms that the data is sent by the target end and uses it to tell the Data Segment Before the serial number of the sending end.
All received. for example, if the validation number is X, it indicates that the previous data segment of the X-1 has been received, only when ACK = 1, the validation number is valid, when ACK = 0, the validation number is invalid, in this case, re-transmission of data is required to ensure data integrity.
SYN synchronization serial number. When TCP establishes a connection, set this position to 1.
The fin sending end completes the sending task bit. When the TCP completes the data transmission, it is proposed that the disconnected party will
Cause:
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.
Another explanation:
This is because the socket in the listen status of the server can respond to ack and SYN after receiving the SYN Packet connection request) it is sent in a message. However, when the connection is closed, when the other party receives the fin Message notification, it only indicates that the other party has no data to send to you; but not all your data may have been sent to the other party, therefore, you may not close the socket immediately, that is, you may need to send some data to the other party, and then send the fin message to the other party to indicate that you agree to close the connection now, therefore, the ACK messages and fin messages are sent separately in most cases.
Why can't two handshakes be connected?
We know that three handshakes are required to complete two important functions: Both parties must prepare for data sending (both parties know that each other is ready), and both parties must allow negotiation 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.
What is the TCP three-way handshake process? Why do I use a three-way handshake? Can I use a two-way handshake?
The client server mode is used to establish a connection. Assume that host a is the client and host B is the server.
1) TCP three-way handshake process: host a sends a connection request to host B, host B confirms the received packet segment of host a, and host a confirms host B again.
2) The three-way handshake is usedPrevents the invalid Connection Request Message segment from being suddenly transmitted to host B..
Invalid Connection Request Message segment means that the connection request sent by host a has not been confirmed by host B, so after a period of time, host a re-sends a connection request to host B, data transmission is completed in sequence. In this special case, the connection request sent by host a for the first time is not lost, but is delayed to reach host B Due to network nodes. Host B considers it a new connection initiated by host, therefore, host B agrees to the connection and sends the confirmation to host a. However, host a does not care at this time. Host B has been waiting for host a to send data, leading to a waste of resources on host B.
Why do TCP connections require three handshakes and four handshakes?