1
头图

table of Contents

  • Glossary
  • TCP three-way handshake

    • Steps for TCP to establish a connection
    • TCP three-way handshake steps
    • Thinking: Why is the TCP handshake not twice or four times?
  • Four waves of TCP

    • TCP disconnect steps
    • TCP's four waved steps
    • Thinking: Why is there an extra step 2 when disconnecting the link?
    • Thinking: Why does the client wait for 2WSL after confirming the disconnection?
  • Interview question: Why does TCP shake hands 3 times and wave 4 times?
This is a very popular and very basic question in a computer network. It is also a frequently asked question in interviews. It is not unusual if you know how to do it, but if you don't, it will be cool. I am here to sort out what I have learned, and the knowledge here should be clear after reading it. First, let's explain the terms first. If you encounter unclear terms, remember to look at them the other way around.

Glossary

  • TCP : TCP In the transmission layer of the computer network model, it corresponds to the transmission from the host to the host, which provides the ability for communication between applications. 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 : It is the concept of the transport layer. A link is a behavior of transmitting data and a record of the state of network behavior. Before transmission, a link is established, which is to establish an object for maintaining the data transmission status in the memory of both the IP data, which records the 060ab4f018e542 and port number of both parties, what the status is, and what is the transmission speed.
  • duplex/simplex :
nameconceptNumber of lines
SimplexAt any one moment, if the data can only be sent in one directionOnly 1 (=1)
Half duplexAt a certain moment, data can be transmitted in one direction, or in the opposite direction to the other, and alternatelyAt least 1 (>= 1)
Full duplexData can be sent and received in both directions at any timeMore than 1 (>1)
  • handshake : TCP handshake aim is to establish a stable link channel.
  • waved : TCP waving purpose is to break the link.
  • WSL(Maximum Segment Lifetime) : The maximum lifetime of a message. TCP allows different implementations to set different WSL values.
  • seq ID : to identify from TCP byte sent by the source to the destination stream, this time labeled initiator 📌 transmission data.
  • ack Determine the serial number : Only when the ACK flag is 1, the confirmation serial number field is valid, ack = seq + 1
  • flag bit:

    • SYN(Synchronization) : One Host actively initiates a connection to another Host , requesting synchronization.
    • ACK(Acknowledgement) : Because of the connection and reliability constraints, the TCP protocol must ensure that every piece of data sent must be returned. After the receiver receives the data, it needs to give the sender a response to confirm that the serial number is in order.
    • RST(Reset) : reset link
    • FIN(Finish) : a Host active disconnect request, the request is completed
    • PSH(Push) : a Host to another Host transmission data, data push
    • ...

TCP three-way handshake

TCP is a relatively complete mechanism to establish a stable transmission channel.

Steps for TCP to establish a connection

Let's take a look at the 6 steps of TCP

  1. (SYN) message to the server 060ab4f018ebf9
  2. The server is ready to connect
  3. (SYN) gives a (ACK) client's 060ab4f018ec45
  4. The server sends a (SYN) to the client
  5. Client is ready
  6. The client sends a (ACK)

Among them, steps 2 and 5 do not require a handshake, and steps 3 and 4 can be combined. So although it takes 6 steps to establish a link, it only needs three handshake, which are 1, 3+4, and 6.

TCP three-way handshake steps

Let's restore the process of the three-way handshake:

TCP的三次握手.jpg

  • At the beginning, both the client and the server are in CLOSED . The client sends SYN=1 to the server to request to establish a link and sends a seq serial number. At this time, the state of the client becomes SYN-SEND .
  • The server receives the message and returns a ACK=1 indicate receipt, and ack confirms the serial number, which is the previous seq serial number +1 , which is x+1 , and the current seq serial number y , and there is a SYN=1 establish a link. It becomes SYN-REVD .
  • After receiving the message, the client sends a ACK=1 to the server to confirm receipt, and a new seq serial number is x+1 , and another ack that the serial number is the previous seq serial number +1 , that is, y+1 . After the completion of the client state programming ESTABLISHED .
  • After the server receives the message, the status also becomes ESTABLISHED , and the link channel is established.

The brief summary is:

  1. Client -> SYN -> Server
  2. Client <- SYN+ACK <- Server
  3. Client -> ACK -> Server

Thinking: Why is the TCP handshake not twice or four times?

Imagine that if there are only two times, the server has not yet determined whether the client is ready, so data transmission cannot be carried out stably. If four times, the server ACK to the client with a ACK according to the client's 060ab4f018efe9, which does not have much effect and causes a waste of resources, which is very unnecessary. Three times exactly can guarantee reliable transmission and improve transmission efficiency.

Four waves of TCP

TCP is intended to disconnect the link state

TCP disconnect steps

  1. (FIN) message to request to disconnect the link 060ab4f018f0a8.
  2. After the server receives the request, it will first respond with (ACK) whether it has received the request.
  3. The server handles the remaining things, such as whether the server has sent any messages, the server may still have sent messages that have not received ACK ; it is also possible that the server has its own resources to release, etc.
  4. The server finishes processing its own affairs and tells the client to close the link (FIN) .
  5. The client receives FIN and handles the things it has done. For example, the client sends a request to the server without receiving ACK .
  6. The client finishes its own business and tells the server to close (ACK) .

Among them, 3 and 5 do not need to wave, but note that here, 2 and 4 cannot be merged. So here you need to wave your hands four times, 1, 2, 4, and 6.

TCP's four waved steps

TCP的四次挥手.jpg

  • At the beginning, the client sends FIN=1 to the server to disconnect, and sends a seq serial number =u . At this time, the client becomes FIN-WAIT1 .
  • After the server receives the message, it returns a ACK=1 confirmation message, and ack confirmation sequence number, which is the previous seq sequence number +1 , that is, u+1 , and a new seq sequence number is v , and the status of the server is now CLOSE-WAIT . After receiving the message, the status will change to FIN-WAIT2 .
  • When the server is ready to close the link, send ACK=1 to the client, and send ack confirm the serial number, the last seq serial number +1 , that is, u+1 , and a new seq serial number w FIN=1 . If there is data that has not been sent before, it will be sent to the client along with this request. At this time, the status of the server becomes LASE-ACK .
  • After the client receives the message, it sends the ACK=1 confirmation message to the server, and also sends the ack confirmation serial number, the last seq serial number +1 , that is, w+1 , and a new seq customer serial number is u+1 , and then The status of the terminal becomes TIME-WAIT , which will continue to 2WSL . If no back-end messages are received during the waiting period, 2WSL will become CLOSED . After the server receives the message, the status becomes CLOSED .

The brief summary is:

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

Thinking: Why is there an extra step 2 when disconnecting the link?

Because when the disconnected server receives FIN , there are more problems to deal with when disconnected, and the link cannot be closed directly. At this time, if the ACK response is not sent to say that the content has been received, the client cannot determine that the message is received by the server No, you can't let the client wait too long in this situation, so you should first reply with a ACK message saying that you have received it. You will have something to deal with later. Wait until the server is finished, and then send a FIN to confirm that the link can be disconnected.

Thinking: Why does the client wait for 2WSL after confirming the disconnection?

  • The first is to ensure that the last ACK message sent by the client can reach the server. If this ACK message is lost, the server will feel that the client has not received the request to disconnect the message I sent, so the server will resend it again. If the client 2MSL time period, it will give a response message again and restart the 2MSL timer.
  • The second is that during this 2WSL time, all the message segments generated during the duration of this link can be eliminated from the network, so that when the new TCP three-way handshake, there will be no invalid request messages in the old link.

Interview question: Why does TCP shake hands 3 times and wave 4 times?

TCP at the transmission layer, corresponding to host-to-host transmission, providing capabilities for application communication, and TCP is a duplex protocol. In order to ensure that both parties establish stable and efficient data transmission, a three-way handshake and four-wave handshake mechanism is used.

three-way handshake are:

  1. SYN to the server to establish a link request (Big brother, can you establish a link?)
  2. The server ACK+SYN package 060ab4f018fa79 as a message reply (Old girl, I have received your message, you can link, have you received it?)
  3. After receiving the message, the client sends ACK as a reply (OK, go) 160ab4f018fadd, this time the data transmission channel is established.

Why not twice, because the client does not return ACK , then the server does not know whether the client has received the message. If it is four times, ACK will return ACK , which wastes bandwidth and is unnecessary.

step four wave is:

  1. The client sends a FIN disconnect request to the server (Brother, I have finished sending everything here, disconnect the link~)
  2. Server receives information, the client sends a ACK reply (old Meier? To break the link? I know you'll wait Son)
  3. After the server has processed the things that need to be processed, it returns FIN to the client (I'm done here, disconnect the link~)
  4. The client receives the message, finishes processing its own affairs, and returns ACK to the server (ok, ~) 160ab4f018fcab, this time the data transmission channel is disconnected.

Why is there four times here? It is because when the disconnected server receives FIN , there are still some things to be processed and it takes some time. At this time, the client cannot be allowed to wait too long, so first reply with a ACK indicate that the message has been received , There is something to deal with here, wait a moment. Wait until the matter is processed, and then send FIN to the client to agree to disconnect the link.

In fact, this is easy to understand. In life, we receive a file or a video sent by the other party. At this time, they need us to reply or comment based on the content. We should first say that the content has been received, let me see and reply to you, so as not to make the other party question whether there is no reply for a long time, whether it has not been received or received or is being watched. After we finish reading, tell each other the result they want.

顽皮的雪狐七七
787 声望74 粉丝

学习探索使我内心平静,