This article is reprinted from the WeChat public account "Java Geek Technology", the author is a fan of Yaxue. Please contact the Java Geek Technology public account to reprint this article. In the past, there were always interviewers who liked to ask, do you know what a three-way handshake is? What is a four-way wave? Why does a handshake require three times and a wave requires four times? Today we will talk about this in detail. 1. What is TCP The TCP protocol, to put it simply, is the Transmission Control Protocol. Why is it called that? Because this protocol is used to control the transmission of data, and I'm sure everyone has no objection to this. TCP is sometimes called "socket" in many books. In fact, this is the translation. In the original book, it may mean a place on a surface or machine with holes for connecting a piece of electrical equipment. Then, after the translator's hand, it is translated into the meaning of socket. In fact, everyone knows it well. There are almost no people who ask about this. So everyone just needs to know it. We all know that network protocols are layered, 7 layers (5 layers), which can be divided into non-standard 7 layers or standard 5 layers (some people say 4 layers, the difference is not big, just one less physical layer). In fact, I personally feel that this standard is still vague and has subtle differences, as shown in this picture. The concept of layering is different. If you divide it according to the OSI seven-layer model structure, it is layer 7, and if you divide it according to TCP/IP, it is layer 4. The TCP here is in our data transmission layer, because after all, as Ah Fen said before, it is the transmission control protocol. 2.TCP protocol message In the TCP/IP layering, even in the data transmission layer, there are non-TCP protocols, such as UDP, as shown in the figure below. TCP and UDP are the two most famous transport layer protocols, both of which use IP as the network layer protocol. Although TCP uses unreliable IP services, the transport layer services it provides are more reliable. Then let's first take a look at what the TCP protocol header looks like. Only by making the abstract things more concrete can we deepen our understanding. TCP data is encapsulated in an IP datagram, as shown in the figure above, and what we need to analyze is the TCP datagram. Each TCP segment contains the source and destination port numbers, which are used to find the sending and receiving application processes. These two values plus the source and destination IP addresses in the IP header uniquely identify a TCP connection. Now we have to see what's inside.
URG Emergency pointer valid ACK Confirms that the sequence number is valid The PSH receiver should hand over this segment to the application layer as soon as possible. RST Reestablish connection SYN Synchronization Sequence Number is used to initiate a connection. This flag and the next flag will be introduced in Chapter 18. FIN The sender completes the sending task
3. What does TCP's three-way handshake connection look like? Since our article is about TCP's three-way handshake and four-way handshake, it must be about connection, not other things. So what does this connection process refer to? Let's understand it from the picture, which is easier to understand.
Simply put, the SYN-SENT state, the synchronous sent state, is the state of the first handshake.
This second handshake will enter the synchronous reception state.
When the third handshake connection is completed, it indicates that the connection has been completely established, and data can be transmitted at this time. Sometimes people will ask, why is it three handshakes instead of two, or four or five? Generally speaking, it is the interviewer who asks this question. If you have already talked about the three handshakes during the interview, he will sometimes ask this question and ask you to talk about your own understanding. So why? The primary reason for using a three-way handshake for TCP connections is stated in RFC 793: the principle reason for the three-way handshake is to prevent old duplicate connection initiations from causing confusion. Translated, the main reason for the three-way handshake is to prevent confusion caused by old repeated connection initiations. That is to say, if the client sends multiple SYN messages to establish a connection in succession, network congestion will occur, and an "old SYN message" will arrive at the server earlier than the "latest SYN" message. At this time, the server will send a SYN + ACK message to the client. After receiving it, the client can determine that this is a historical connection (serial number expired or timed out) based on its own context, then the client will send a RST message to the server, indicating that the connection is terminated. If it is a two-way handshake, then it's over. At this time, it is impossible to determine whether this connection is a historical connection, interrupted or not, and there is no way to handle it. However, a three-way handshake can provide enough context to determine whether this connection is a historical connection when the client sends a message for the third time. So why not four connections? You can continue to turn to the above figure. If it is a four-way connection, it means that ACK and SYN are separated, and the two steps of seq=y and ack=x+1 are separated. Although four-way handshakes can also complete this step, in order to save trouble, people still do it in three steps. In this way, it can also ensure that the initial sequence numbers of both parties can be reliably synchronized. Why spend one more step? 4. Four waves of TCP connection Since we performed three handshakes during TCP connection, why do we have to wave hands four times when it is interrupted? We still have to understand this from the picture.
So why four times? When I was interviewing someone before, a buddy told me that since TCP connection is full-duplex, each direction must be closed separately. This was a general statement, but it didn't explain clearly why we had to wave our hands four times, which was the most embarrassing. Then I asked him what it looked like in detail, and he gave a good answer. So why? This is because when the server's SOCKET in the LISTEN state receives the connection request of the SYN message, it can send ACK and SYN (ACK is a response, and SYN is a synchronization) in one message. But when closing the connection, when receiving the other party's FIN message notification, it only means that the other party has no data to send to you; but not all your data may be sent to the other party, so you may not close the SOCKET immediately, that is, you may need to send some data to the other party before sending a FIN message to the other party to indicate that you agree to close the connection, so the ACK message and FIN message here are sent separately in most cases. That is to say, when closing, in order to confirm whether to close the connection, the ACK message and the FIN message are sent separately. At this time, the number of waves changes from three to four. Is this easier to understand? 5. Article references
|
<<: Before 5G mobile phones become popular, these problems must be solved first
>>: Three misconceptions about 5G
Some time ago, Huawei's "Intelligent Wor...
Recently, Ruijie Networks released two new servic...
5G has not yet been fully rolled out, but 6G is a...
"Industry and Information Technology V News&...
Networks are increasingly reliant on software and...
Translator profile: Zheng Minxian works for Nooyu...
1. Development History: 2. Wireless network class...
A global pandemic, the resulting recession and na...
Since the beginning of the 21st century, cloud co...
As a developing country, China's internationa...
On June 28, 2023 MWC Shanghai opened, and Huawei ...
By 2018, 5G has gone from theory to reality and w...
Global technology market advisory firm ABI Resear...
What is a local area network? The so-called local...
2020 is a critical year for my country's 5G c...