I first studied communications major. After graduation, my classmates went their separate ways to pursue their dreams and traveled between construction sites of all sizes. Haha, just kidding, there are also some amazing people who entered a certain research institute, huh? His father is not the director, so he should not be too dark inside. I remember there was a very high-level course called Computer Network (probably this name). There is a concept about handshake, and now review it.
Let’s take a look at the schematic first:
TCP is connection-oriented. No matter which direction the other party sends data, a connection must be established between the two parties. In the TCP/IP protocol, the TCP protocol provides reliable connection services, and the connection is initialized through three handshakes. The purpose of the three-way handshake is to synchronize the serial number and confirmation number of both parties and exchange TCP window size information.
1. First handshake: establish a connection. The client sends a connection request message segment, sets the SYN position to 1, and the Sequence Number is x; then, the client enters the SYN_SEND state and waits for the server to confirm;
2. Second handshake: The server receives the SYN message segment. When the server receives the SYN message segment from the client, it needs to confirm the SYN message segment and set the Acknowledgment Number to x+1 (Sequence Number+1); at the same time, it also needs to send the SYN request information, set the SYN position to 1 and the Sequence Number to y; the server puts all the above information into a packet segment (i.e., the SYN+ACK message segment) and sends it to the client together. At this time, the server enters the SYN_RECV state;
3. The third handshake: The client receives the SYN+ACK packet segment of the server. Then set the Acknowledgment Number to y+1 and send an ACK message segment to the server. After the message segment is sent, both the client and the server enter the ESTABLISHED state and complete the TCP three-time handshake.
After completing three handshakes, the client and server can start transmitting data. The above is the overall introduction to TCP’s three-time handshake.
What about those four waves?
After the client and the server establish a TCP connection through three handshakes, when the data transmission is completed, the TCP connection must be disconnected. Then there is a mysterious "four waves" here for the disconnection of TCP.
1. The first wave: Host 1 (can be used as a client or as a server), set the Sequence Number and Acknowledgment Number, and send a FIN message segment to Host 2; at this time, Host 1 enters the FIN_WAIT_1 state; this means that Host 1 has no data to be sent to Host 2;
2. The second wave: Host 2 received the FIN message segment sent by Host 1, and returned an ACK message segment to Host 1. The Acknowledgment Number is the Sequence Number plus 1; Host 1 enters the FIN_WAIT_2 state; Host 2 tells Host 1 that I have no data to send, so I can close the connection;
3. The third wave: Host 2 sends a FIN message segment to Host 1, requesting to close the connection, and at the same time, Host 2 enters the CLOSE_WAIT state;
4. The fourth wave: Host 1 receives the FIN message segment sent by Host 2, sends the ACK message segment to Host 2, and then host 1 enters the TIME_WAIT state; after host 2 receives the ACK message segment of Host 1, it closes the connection; at this time, host 1 still does not receive a reply after waiting for 2MSL, it proves that the Server side has been closed normally, so it is OK, host 1 can also close the connection.
At this point, TCP's four waves were completed happily. When you see this, you will have a lot of questions in your mind, a lot of don’t understand, and it feels messy; it’s okay, let’s continue to summarize.
Why do you need to shake hands three times?
The so-called three-Way Handshake means establishing a TCP connection, which means that when establishing a TCP connection, the client and server need to send a total of 3 packets to confirm the establishment of the connection. In socket programming, this process is triggered by the client executing connect.
Since we have summarized the three handshakes of TCP, why do we have to do three times? Why do we think we can complete it in two times? So why does TCP have to make three connections? This is what Xie Xiren said in "Computer Network":
In order to prevent the failed connection request segment from being suddenly transmitted to the server, an error occurs.
In the book, an example is given as follows:
The generation of "failed connection request segment" is in such a case: the first connection request segment sent by the client is not lost.
Instead, it stays in a certain network node for a long time, resulting in delays until a certain time after the connection is released before reaching the server. This was originally a
A long time ago failed message segment. However, after the server received this invalid connection request message segment, it mistakenly thought that it was a new one sent by the client again
connection request. So acknowledgement segment was sent to the client and agreed to establish the connection. Assuming that "three-time handshake" is not adopted, then only server
Issue a confirmation and the new connection is established. Since the client has not issued a request to establish a connection, the server's confirmation will not be ignored.
No data will be sent to the server either. But the server thought that the new transportation connection had been established and was waiting for the client to send data. so,
Many of the server's resources are wasted. The above phenomenon can be prevented from happening by using the "three-time handshake". For example, the situation just now,
The client will not issue acknowledgment to the server's acknowledgment. Since the server cannot receive the confirmation, it knows that the client does not require the connection to be established. "
This is very clear, which prevents the server from waiting and wasting resources.
Why do you have to wave four times?
Why do you wave four times?
The so-called four-way Wavehand is to terminate the TCP connection, which means that when a TCP connection is disconnected, the client and server need to send a total of 4 packets to confirm the disconnection of the connection. In socket programming, this process is triggered by either the client or the server executing close.
The TCP protocol is a connection-oriented, reliable, byte stream-based transportation layer communication protocol. TCP is full duplex mode, which means that when host 1 sends a FIN segment, it just means that host 1 has no data to be sent. Host 1 tells host 2 that its data has been sent; however, at this time, host 1 can still accept data from host 2; when host 2 returns the ACK segment, it means that it already knows that host 1 has no data to be sent, but host 2 can still send data to host 1; when host 2 also sends a FIN segment, it means that host 2 has no data to be sent, and it will tell host 1 that I have no data to be sent, and then each other will happily interrupt this TCP connection. If you want to correctly understand the principle of four waves, you need to understand the changes in the state during the four waves.
FIN_WAIT_1: This state needs to be explained carefully. In fact, the true meaning of the FIN_WAIT_1 and FIN_WAIT_2 states are both to wait for the other party's FIN message. The difference between these two states is: the FIN_WAIT_1 state is actually when SOCKET is in the ESTABLISHED state, it wants to actively close the connection and send a FIN message to the other party. At this time, the SOCKET enters the FIN_WAIT_1 state. When the other party responds to the ACK message, it enters the FIN_WAIT_2 state. Of course, under actual normal circumstances, no matter what the other party is in, the ACK message should be immediately responded to the ACK message. Therefore, the FIN_WAIT_1 state is generally difficult to see, and the FIN_WAIT_2 state can often be seen with netstat. (Active Party)
FIN_WAIT_2: This state has been explained in detail above. In fact, SOCKET in the FIN_WAIT_2 state means semi-connection, that is, one party requires a close connection, but also tells the other party that I have some data to be transmitted to you (ACK information) for the time being, and then close the connection later. (Active Party)
CLOSE_WAIT: The meaning of this state is actually to be waiting to close. How to understand? When the other party closes a SOCKET and sends a FIN message to yourself, your system will undoubtedly respond to an ACK message to the other party, and then it will enter the CLOSE_WAIT state. Next, what you really need to consider is to check whether you still have data to send to the other party. If not, then you can close the SOCKET and send FIN messages to the other party, that is, close the connection. So when you are in CLOSE_WAIT state, what you need to do is wait for you to close the connection. (Passive side)
LAST_ACK: This state is relatively easy to understand. It passively closes the ACK message of the other party after sending the FIN message. After receiving the ACK message, you can enter the available state of CLOSED. (Passive side)
TIME_WAIT: It means that the other party's FIN message has been received and the ACK message has been sent. Just wait for 2MSL to return to the available state of CLOSED. If in FINWAIT1 state, when receiving a message with the other party with both the FIN flag and the ACK flag, you can directly enter the TIME_WAIT state without going through the FIN_WAIT_2 state. (Active Party)
CLOSED: Indicates that the connection is interrupted.
Summarize
The above is the entire content of this article on the principle and process analysis of three handshakes and four handshakes in the TCP/IP protocol. I hope it will be helpful to everyone. If there are any shortcomings, please leave a message and look forward to your valuable opinions.