“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

preface

TCP is one of the most common questions in today’s job interviews. Do you know TCP? Do you know why a TCP connection requires a three-way handshake? What about the four waves? Although we have a general understanding of the answer, and silently in the interview after interview, know how to answer this question, but we really know TCP? Today, we will start with three handshakes and one more wave (four waves). At the end of the article, you can ask the interviewer next time, do you really know TCP? (Dog head to save life)

TCP’s past and present life

In MDN, TCP is defined as follows:

Transmission Control Protocol (TCP) is one of the major network protocols. It enables two hosts to establish a connection and exchange data flows. TCP ensures the delivery of data and maintains the order in which packets are sent. TCP was designed in the 1970s by two DARPA scientists, Vint Cerf and Bob Kahn, who were then called the fathers of the Internet.Copy the code

From the above definition we find several things:

  • TCP is one of the network protocols
  • TCP Connects two hosts and exchanges data
  • TCP transmits data in a certain order

TCP: Transmission control protocol

TCP is the Transport Control Protocol. It is a host-to-host connection oriented Protocol. What is mainframe to mainframe? Between our two devices, including but not limited to PC, phone, tablet, etc. And TCP is a protocol that supports full duplex. What is duplex? Just like our one road has two back and forth, and the corresponding simplex is like our one-way road, only from this end to that end, so, with the support of duplex, our two hosts can send messages to each other.

So having said that, now that we know that TCP is really a connection that Bridges communication between applications, why do we use TCP? What are the benefits of using TCP? In fact, THE transmission of TCP is very reliable. Why? Because that’s the three handshake and four wave guarantee that we need to have, which is lossless transmission, where each packet is sent in order, and you need to give a feedback when you receive it, and if you don’t get any feedback after a certain period of time, you’ll resend it until you get the feedback.

TCP– Three-way handshake

Finally, it’s time to shake hands three times. If you haven’t heard of it, you’re going to be confused. Oh, three times. So friendly, so polite? TCP is a reliable protocol, because before sending data, we first need to establish a connection, and TCP itself needs to carry parameters. For example, when establishing a connection, the application needs to carry the SYN. If the receiver wants to show that the message has been received, he should carry an ACK when he gives back. If you need to push data to another party, it is also called PSH; If one party needs to disconnect, then send FIN, let’s take a look at the following picture, it may be easy to understand:

What is the function of SYN, ACK, etc?

The connection between the two applications needs to be established. First, A sends A message to B with SYN and SEq =x, and then B replies with A SEQ and an ACK in response to the last sent message, adding 1 to x to indicate that this message is the reply. After receiving the message, A will reply to it again. Ok, SEQ and ACK are the same as those indicated above. So far, A and B have completed three waves and established the connection, and both sides can send and receive messages.

Don’t understand? It doesn’t matter, read the following xiaoming and the goddess of the story you understand.

Xiao Ming first launched a invitation to the goddess, the goddess of but didn’t respond to him, he found received message after some time, he will resend again to the goddess, the goddess of the received, and euphemism refused him, xiao Ming said to know, at this point, xiao Ming and small red chat heaven, as a fan of the goddess, must not miss the opportunity to chat with my goddess!

TCP– four waves

After the three handshakes, let’s talk about four waves, a wave of the hand without taking away a cloud. I’m sorry. Get back to business, TCP disconnected why four times, the whole process is actually and almost three times, the first is A to B said, I’m going to disconnect, B I know, and then B sends me to disconnect, this is the last message, A finally sent back to one, has confirmed that is the last message, disconnected, we look at the below:

As shown, we found A sends information to the B A FIN ends, seq = u, B in the treatment of after his request to hand back A for you the end of the message I received just now, I have finished processing the things at hand, and the corresponding seq u + 1, and then also send A FIN sign to A, B says I have to disconnect, Seq =w, seq= U +1, and then B disconnects. After A does not receive A message from B for A period of time, A disconnects. At this point, the communication between A and B ends.

Do you not understand, it doesn’t matter, the story of xiao Hong and Xiao Ming also want to end.

Small red found his rich childe, natural want to shake off small bright, said goodbye with small bright, concurrent good person card, small bright uncomfortable of express I accepted, then the last blessing small red, small red side say you also, while thinking finally shake off a spare tire, ah, I this damn charm!

conclusion

At this point, the story of Xiao Ming and Xiao Hong is over. By the way, it should be emphasized that the story of Xiao Ming and the goddess in the figure is only used to explain the relationship between the host and the host between the three and four times, which belongs to the session of the application layer, not the connection of the transport layer, we should not confuse oh. Our understanding of the TCP over, also believe that experienced a graphic description of the above, you must have had a deep impression to the TCP, if can’t remember, imagine xiao Ming’s experiences, believe that you will be prompted to you, the next time the interview, if the interviewer asks about the TCP, we gave him tells a story of xiao Ming, magny cours, xiao zhi Daniel, I believe the interviewer will be impressed by you and succeed in winning the offer!