A fan who has worked for 5 years, recently went to interview with many companies, and every time he was asked incoherently by various technical principles.
Since I was looking for a job for almost a month, I was a little anxious and came to me for help.
All I can do is make sure to update one interview question a day and ask him what was the one that impressed him the most, he said.
"Why does the TCP protocol design a three-way handshake".
Ok, on this question, let's take a look at the answers of ordinary people and experts.
Ordinary people:
I think it is because TCP is a duplex protocol, which means that it can transmit data in both directions.
So since it is a two-way transmission of data, it means that both ends of me establish a communication connection. Both ends are connected in both directions with each other. All is equivalent to saying that A wants to establish a connection like B, and B wants to establish a connection like A, which is twice in between.
In addition, in the process of establishing a connection, I also need to confirm whether the connection is successfully established. So when the client sends to the server to establish a connection, it needs to give a response.
Expert:
Regarding this question, I will answer it from the following three aspects.
The TCP protocol is a reliable, byte stream-based, connection-oriented transport layer protocol.
- Reliability is reflected in that the data transmission between the two parties in the TCP protocol communication is stable. Even in the case of poor network, TCP can ensure that the data is transmitted to the target end, and this reliability is achieved based on the packet confirmation mechanism.
- The data transmission between the two sides of the TCP communication is realized through the byte stream.
- Connection-oriented means that before data transmission, a connection must be established, and then data transmission is performed based on this connection.
Because TCP is a connection-oriented protocol, a reliable connection needs to be established before data communication. TCP uses a three-way handshake to establish a connection.
The so-called three-way handshake means that both parties need to send a total of three requests to ensure the establishment of the connection.
- The client sends a connection request to the server and carries the synchronization sequence number SYN.
- After the server receives the request, it sends SYN and ACK, where SYN represents the synchronization sequence number of the server, and ACK represents an acknowledgment of the previously received request, which means to tell the client that I have received your request.
- After the client receives the request from the server, it sends an ACK again. This ACK is an acknowledgment for the connection of the server, which means to tell the server that I have received your request.
The reason why TCP has to design a three-way handshake, I think there are three reasons:
- TCP is a reliable communication protocol, so both parties of the TCP protocol must maintain a sequence number to mark the data packets that have been sent and which ones have been signed by the other party. The three-way handshake is that the two parties communicate the starting value of the serial number to each other. In order to ensure that the serial number is received, both parties need to have a confirmation operation.
- The TCP protocol needs to achieve reliable data transmission in an unreliable network environment, which means that both parties must use some means to achieve a reliable data transmission channel, and three communications is the minimum value for establishing such a channel. Of course, it can be four or five times, but there is no need to waste this resource.
- To prevent the confusion caused by repeated connection initialization in history, for example, in the case of poor network, the client sends a request to establish a connection multiple times in a row. Assuming that there are only two handshakes, the server can only choose to accept or reject the connection request. , but the server does not know whether this request is a previous request that expired due to network congestion, that is to say, the server does not know whether the current client connection is valid or invalid.
The above is my understanding of the problem.
Summarize
The content of network communication is still relatively important. When facing some online network troubleshooting,
It can quickly help us locate the problem and find a solution.
If you have any interview questions, career development questions, or study questions, you can send me a private message. There are also Java interview materials and resume templates that you can get for free.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。