directory

  • Noun explanation
  • TCP’s three handshakes

    • TCP steps to establish a link
    • TCP’s three handshake steps
    • Consider: Why not two or four TCP handshakes?
  • TCP’s four waves

    • Steps for TCP disconnection
    • TCP’s four wave steps
    • Consider: Why do you need an extra step 2 when disconnecting?
    • Consideration: Why wait for 2WSL after the client finally confirms the disconnect?
  • Interview question: TCP why is 3 handshakes, 4 waves?

This is one of the most popular, basic questions in computer networks, and one of the most frequently asked questions in interviews. If you know it, it’s not unusual. If you don’t, it’s cool. I’m just going to do a little bit of a review of what I’ve learned, and it should be clear when I finish reading it. First, explain the nouns first. If you encounter unclear nouns, remember to look backwards.

Noun explanation

  • TCP:TCPAt the transport layer of the computer network model, the corresponding host-to-host transport provides the capability for inter-application communication.TCP Is a duplex protocol, data can be transmitted in both directions at any time, which means that the client and the server can send and receive information equally.
  • link: is the concept of transport layer, link is a kind of data transmission behavior, is the network behavior state record. Before transmission, a link is established in the memory of both receiving and receiving data to maintain the status of the data transmission object, which records the two sidesIPAnd port number, state, transmission speed and so on.
  • Duplex/simplex:
The name of the concept Number of lines
simplex At any one time, if the data can only be sent one way We only need one bar (=1).
Half duplex Data can be transmitted in one direction at a time, and in the other direction, alternately At least 1 bar (>= 1)
Full duplex Data can be sent and received in both directions at any time More than one bar > one.
  • Shake hands:TCPThe purpose of the handshake is to establish a stable link channel.
  • wave:TCPThe purpose of the wave is to break the link.
  • WSL (Maximum Segment Lifetime) : The Maximum Lifetime of a packet. TCP allows different implementations to set different WSL values.
  • seqThe serial number: Used to identify fromTCPThe stream of bytes sent from the source to the destination, marked 📌 when the initiator sends data.
  • ackMake sure the serial number: onlyACKWhen the flag is 1, the confirmation ordinal field is valid.ack = seq + 1
  • Mark:

    • The SYN (Synchronization)A:HostTake the initiative to one anotherHost Initiate a connection and request synchronization.
    • ACK (Acknowledgement): Since connection and reliability constraints are to be maintained,TCP The protocol ensures that every piece of data sent must be returned. After receiving the data, the receiver needs to give the sender a response confirmation that the serial number is in order.
    • RST (Reset) : Reset the link
    • FIN (Finish)A: Host Proactively disconnect request, request completed
    • PSH (Push)A:Host To another Host Sending data, data push
    • .

TCP’s three handshakes

TCP’s three handshakes, relatively speaking, is a relatively complete mechanism, aimed at establishing a stable transmission channel.

TCP steps to establish a link

Here’s a look at the six steps that TCP takes to establish a link:

  1. The client sends a message to the server(SYN)
  2. The server is ready to connect
  3. The server is against the client(SYN)Give me a(ACK)
  4. The server sends one(SYN)To the client
  5. The client is ready
  6. The client sends one to the server(ACK)

Steps 2 and 5 do not need to shake hands, while steps 3 and 4 can be combined together. So even though it takes 6 steps to create a link, it only takes 3 handshakes, 1,3 + 4,6.

TCP’s three handshake steps

Here’s the process of three handshakes:

  • Start with both the client and the serverCLOSEDThe state that the client sendsSYN=1Create a link to the server representation request and send oneseqSerial number, at which point the state of the client becomesSYN-SEND.
  • The server receives a message and returns oneACK=1It’s an acknowledgement of receipt, and there’s an ACK to confirm the serial number, which is the previous oneseqThe serial number+ 1, i.e.,x+1, and this timeseqThe serial numberyHere’s another oneSYN=1Create the link, at which point the server state becomesSYN-REVD.
  • When the client receives the message, it sends one to the serverACK=1It’s acknowledgement. There’s a new oneseqThe serial number isx+1Here’s another oneackMake sure the sequence number is previousseqThe serial number+ 1, i.e.,y+1. After the completion of the client state programmingESTABLISHED.
  • After the server receives the message, the state also becomesESTABLISHED, link channel establishment.

The short summary is:

  1. Client -> SYN -> server
  2. Client < -syn +ACK < -server
  3. Client -> ACK -> server

Consider: Why not two or four TCP handshakes?

Imagine that if there are only two times, the server has not yet determined whether the client is ready, so the data transfer cannot be stable. If four times, the server will reply an ACK to the client based on the ACK of the client, it will not have much effect and cause a waste of resources, so it is not necessary. Three times is just enough to ensure reliable transmission and improve transmission efficiency,

TCP’s four waves

TCP’s wave is designed to break the link state

Steps for TCP disconnection

  1. The client sends a message asking to disconnect(FIN).
  2. After receiving the request, the server will first respond to whether it has received the request(ACK).
  3. The server handles the remaining things, such as the server still has not sent the message, the server may still have sent the message not received ACK; It is also possible that the server itself has resources to release, etc.
  4. The server finishes its work and tells the client it can close the link(FIN).
  5. Client receivedFIN, to handle the things they have done, such as the client has sent to the server did not receiveACK “, etc.
  6. The client handles its own business and tells the server it can close(ACK).

3 and 5 don’t need to wave, but notice here that 2 and 4 can’t merge. So we need four waves here, one, two, four, six.

TCP’s four wave steps

  • Start sending from the client to the serverFIN=1To break the link, and sent oneseqThe serial number=uAt this point the client becomesFIN-WAIT1.
  • After the server receives the message, it returns oneACK=1Confirmation and moreackConfirm the serial number. It’s the previous oneseqThe serial number+ 1, i.e.,u+1And there’s a new oneseqThe serial number forv, the server state becomesCLOSE-WAITAfter the client receives the message, the state will change toFIN-WAIT2.
  • When the server is ready to close the link, send it to the clientACK=1, and also sendackConfirm the number, go upseqThe serial number+ 1, i.e.,u+1And there’s a new oneseqThe serial number forwI’m going to send oneFIN=1. If there is any data that has not been sent before, it will be sent to the client following the request. The server state then becomesLASE-ACK.
  • After the client receives the message, it sends everything it has here to the serverACK=1The confirmation message was also sentackConfirm the number, go upseqThe serial number+ 1, i.e.,w+1And there’s a new oneseqThe serial number foru+1Then the state of the client becomesTIME-WAITThis state will continue2WSLAnd if no more messages are received from the back end during the waiting period,2WSLAnd then it becomesCLOSED. After the server receives the message, the state also becomesCLOSED.

The short summary is:

  1. Client -> FIN -> Server
  2. Client < -ack < -server
  3. Client < -fin < -server
  4. Client -> ACK -> server

Consider: Why do you need an extra step 2 when disconnecting?

Because the unlink the server receives the FIN, the more disconnected to deal with the problem, not directly close links, this time if you don’t send an ACK response is received, the client is unable to determine the news server received no, can’t let the client in this situation for too long, so you should reply before an ACK message is received, I have a few things to take care of later. When the server is done, send a FIN to make sure it is ready to disconnect.

Consideration: Why wait for 2WSL after the client finally confirms the disconnect?

  • The first is used to ensure that the client sends the last oneACKA packet can reach the server if thisACKSo if the message is lost, the server will think that the client didn’t receive the request that I sent and disconnect the message, so the server will resend it if the client is here2MSLIf a message is retransmitted within that time frame, a response message is given again and it is restarted2MSLThe timer.
  • The second is in this2WSLDuring the duration of the link, all packets generated during the duration of the link can be erased from the network, so that the newTCPThere are no invalid request packets from the old link when the handshake is repeated three times.

Interview question: TCP why is 3 handshakes, 4 waves?

TCP in the transport layer, corresponding to host-to-host transmission, provides the ability for application communication, and TCP is a duplex protocol, in order to ensure that both sides establish stable and efficient data transmission, the use of three handshakes and four waves of the working mechanism.

The steps for three handshakes are:

  1. The client sends to the serverSYNA request to establish a linkBig brother, can you establish a link?
  2. The server is going toACK+SYNPackage a reply as a messageOld sister, I received your message, can carry on the link, you received?
  3. The client receives the message and sends it to the serverACKAs a reply(OK, start ~), at which time the data transmission channel is established.

Because the client does not return an ACK, the server does not know whether the client received the message or not. If it returns an ACK four times, it will return an ACK based on the ACK, which is a waste of bandwidth and unnecessary.

The steps for the four waves are:

  1. The client sends to the serverFINDisconnect request(Big brother, my side of things have finished, disconnect the link ~)
  2. The server receives the message and sends one to the clientACKTo reply(Sweetie? Disconnect? I see. Please hold on for a minute.
  3. The server returns when it has finished processingFINTo the client(I'm done, please disconnect the link.)
  4. The client receives the message, finishes its own business, and returnsACKTo the server(OK, break it.)At this time, the data transmission channel is disconnected.

The reason why it is four times here is that when the server receives the FIN after the disconnection, it still has some things to deal with, which will take some time. At this time, the client cannot wait too long, so reply an ACK first to indicate that the message has been received, and there is something to be processed here. Please wait a moment. Wait until the matter is resolved, and then send the client FIN to agree to disconnect the link.

In fact, this is easy to understand. In our life, when we receive a file or a video sent by the other party, they need us to reply or comment on it according to the content. We should first say that the content received, I see to give you a reply, so that the other side will not have a question for a long time did not reply is not received or received is looking. We’ll tell each other what they want after we’ve seen it.