directory

  • Noun explanation
  • TCP three-way handshake
    • TCP steps for establishing a link
    • TCP three-way handshake procedure
    • Consider: why not two or four TCP handshakes?
  • TCP’s four waves
    • Procedure for TCP disconnection
    • TCP’s four wave steps
    • Consider: Why is there an extra step 2 when disconnecting?
    • Ponder: Why wait for 2WSL after the client finally confirms the disconnection?
  • Interview question: why is TCP three handshakes, four waves?

This is a very popular, very basic question in a computer network. It is also a question often tested in the interview. If you know it, it is not unusual. I’m here to sort out what I’ve learned, and it should be pretty clear when I finish. Let’s start with a noun explanation. If you don’t have a clear noun, remember to look over your shoulder.

Noun explanation

  • TCP: At the transport layer of the computer network model, TCP corresponds to host-to-host transmission and provides inter-application communication capabilities. TCP is a duplex protocol. Data can be transmitted in both directions at any time, which means that the client and server can send and receive information equally.

  • Link: the concept of transport layer, link is a kind of data transmission behavior, is the record of network behavior state. Before transmission, a link is established, which is to establish an object for maintaining the data transmission state in the memory of both data transceivers and receivers, which records the IP and port number of both parties, how the state is, how the transmission speed is, etc.

  • Duplex/simplex:

The name of the concept Number of lines
simplex At any given moment, if the data can only be sent one way I only need one (=1)
Half duplex Data can be transmitted in one direction at a time, or in the other direction, alternately At least 1 item (>= 1)
Full duplex Data can be sent and received in both directions at any time More than 1 item (>1)
  • Shake hands:TCPThe purpose of the handshake is to establish a stable link channel.
  • wave:TCPThe purpose of the wave is to disconnect the link.
  • Maximum Segment Lifetime (WSL) : indicates the Maximum Segment Lifetime of packets. TCP allows different implementations to set different WSL values.
  • seqThe serial number: Identifies the slaveTCPByte stream sent from the source to the destination, marked by the initiator when sending data 📌.
  • ackMake sure the serial number: onlyACKWhen the flag is 1, confirm that the serial number field is valid.ack = seq + 1
  • Mark:
    • The SYN (Synchronization)A:HostReach out to the otherHost Initiate a connection and request synchronization.
    • ACK (Acknowledgement): Because of maintaining connectivity and reliability constraints,TCP The protocol ensures that every piece of data sent must be returned. After receiving the data, the receiver needs to send a response to the sender to confirm that the serial number is in order.
    • RST (Reset) : resets the link
    • FIN (Finish)A:HostDisconnect the request and the request is complete
    • PSH (Push)A:Host To anotherHostSend data, data push
    • .

TCP three-way handshake

TCP’s three-way handshake is a relatively complete mechanism designed to establish a stable transmission channel.

TCP steps for establishing a link

Here’s a look at the 6 steps TCP takes to establish a connection:

  1. The client sends a message to the server(SYN)
  2. The server is ready to connect
  3. The server is for 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 require a handshake, while steps 3 and 4 can be combined. So it takes 6 steps to establish a link but only 3 handshakes, 1,3 + 4,6.

TCP three-way handshake procedure

Let’s restore the three-way handshake:

  • Start both client and server are inCLOSEDThe client sends the statusSYN=1Requests a link to the server and sends oneseqSequence number, at which point the client state becomesSYN-SEND.
  • The server returns a messageACK=1Acknowledge receipt, and confirm ACK number, the previous oneseqThe serial number+ 1, i.e.,x+1And this oneseqThe serial numberyAnd one moreSYN=1The link is established, at which point the server state becomesSYN-REVD.
  • When the client receives the message, it sends one to the serverACK=1Acknowledge receipt and a new oneseqThe serial number isx+1And one moreackMake sure the serial number is the previous oneseqThe serial number+ 1, i.e.,y+1. After completing the client state programmingESTABLISHED.
  • When the server receives the message, the state becomesESTABLISHEDThe link channel is established.

The brief 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 is not stable. If the server replies to the client with an ACK for four times according to the ACK of the client, it does not have much effect and causes a waste of resources, it is very unnecessary. Three times can guarantee reliable transmission and improve transmission efficiency.

TCP’s four waves

The TCP wave is intended to break the link state

Procedure for TCP disconnection

  1. The client sends a message to disconnect the link(FIN).
  2. After receiving the request, the server responds to whether it received the request(ACK).
  3. The server handles the rest, such as messages that the server has not yet sent, or messages that the server has not yet received ACK; The server may also have resources to release.
  4. The server is done with its business and tells the client it can close the link(FIN).
  5. The client receivesFINTo handle things that are done by the client, such as sending something to the server but not receiving itACK Request, etc.
  6. The client process completes its business and tells the server it can shut down(ACK).

3 and 5 don’t need to be waved, but notice here, 2 and 4 can’t be combined. So we need four waves here, one, two, four, six.

TCP’s four wave steps

  • The client sends a message to the serverFIN=1To disconnect the link, and send oneseqThe serial number=uAt this point the client becomesFIN-WAIT1.
  • When the server receives the message, it returns oneACK=1Confirm the news, andackConfirm the serial number. It’s the last oneseqThe serial number+ 1, i.e.,u+1And a new oneseqThe serial number forv, the server status changes toCLOSE-WAITAfter the client receives the message, the status becomesFIN-WAIT2.
  • Send it to the client when the server has everything ready to close the linkACK=1And sendackConfirm the serial number. Up oneseqThe serial number+ 1, i.e.,u+1And a new oneseqThe serial number forwAnd send one moreFIN=1. If there is any data that has not been sent before, it will be sent to the client with this request. The state of the server becomesLASE-ACK.
  • After receiving the message, the client completes its own contents and sends them to the serverACK=1A confirmation message was sentackConfirm the serial number. Up oneseqThe serial number+ 1, i.e.,w+1And a new oneseqThe serial number foru+1And then the client state becomesTIME-WAITAnd it will continue2WSLIf no message is received from the back end during the waiting period,2WSLAnd then it becomesCLOSED. After the server receives the message, the status becomesCLOSED.

The brief summary is:

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

Consider: Why is there 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 need you to take care of something later. Wait until everything on the server is finished, then send a FIN to confirm that the connection can be disconnected.

Ponder: Why wait for 2WSL after the client finally confirms the disconnection?

  • The first is used to ensure that the client sends the last oneACKThe message can reach the server if thisACKIf the packet is lost, the server will think that the client did not receive the disconnection request that I sent, and the server will send it again, if the client is in this2MSLAfter receiving retransmitted packets within a period of time, the system sends a reply packet again and restarts2MSLThe timer.
  • The second is in this2WSLTime, you can make all message segments generated during the duration of this link disappear from the network, so that the newTCPIn the three-way handshake, invalid request packets in the old link do not appear.

Interview question: why is TCP three handshakes, four waves?

At the transport layer, TCP corresponds to host-to-host transmission and provides the capability for application communication. TCP is a duplex protocol. To ensure stable and efficient data transmission between both parties, TCP uses the three-way handshake and four-way handshake.

The steps of the three-way handshake are:

  1. The client sends a message to the serverSYNRequest to establish a link(Big brother, can you establish a link?)
  2. The server needs to change theACK+SYNPackage it as a reply to a message(old sister, I have received your message, you can link, have you received?)
  3. The client receives the message and sends it to the serverACKAs a reply(Ok, let's go.)At this time the data transfer channel is established.

The reason is that the client does not return an ACK, so the server does not know whether the client has received the message. If it returns an ACK for four times, it wastes bandwidth and is unnecessary to return an ACK for four times.

The steps for four waves are:

  1. The client sends a message to the serverFINA request to disconnect the link(Eldest brother, I have sent all the things here, please disconnect the link ~)
  2. The server receives the message and sends one to the clientACKTo reply(Sister? Want to disconnect? I see. Just a moment, please.
  3. The server completes what needs to be done and returnsFINTo the client(I'm done here, please disconnect the link ~)
  4. The client receives the message, does its own thing, and returnsACKTo the server(Ok, break it.)At this time, the data transmission channel is disconnected.

The reason why it is four times is that when the disconnected server receives the FIN, there are still some things to deal with, which takes some time. At this time, the client cannot wait too long. Therefore, it replies with an ACK first to indicate that the message has been received, and there is something to deal with here. Wait until the matter is resolved, and then send the client FIN agreeing to disconnect the connection.

In fact, this is easy to understand. In life, when we receive a document 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 has been received, and I will check and reply to you, so that the other party will not have questions about whether the reply has not been received or received. We’ll tell each other what they want after we read it.