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
:TCP
At 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 sides
IP
And 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:
TCP
The purpose of the handshake is to establish a stable link channel. - wave:
TCP
The 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.
seq
The serial number: Used to identify fromTCP
The stream of bytes sent from the source to the destination, marked 📌 when the initiator sends data.ack
Make sure the serial number: onlyACK
When the flag is 1, the confirmation ordinal field is valid.ack = seq + 1
-
Mark:
The SYN (Synchronization)
A:Host
Take 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 completedPSH (Push)
A:Host
To anotherHost
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:
- The client sends a message to the server
(SYN)
- The server is ready to connect
- The server is against the client
(SYN)
Give me a(ACK)
- The server sends one
(SYN)
To the client - The client is ready
- 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 server
CLOSED
The state that the client sendsSYN=1
Create a link to the server representation request and send oneseq
Serial number, at which point the state of the client becomesSYN-SEND
. - The server receives a message and returns one
ACK=1
It’s an acknowledgement of receipt, and there’s an ACK to confirm the serial number, which is the previous oneseq
The serial number+ 1
, i.e.,x+1
, and this timeseq
The serial numbery
Here’s another oneSYN=1
Create the link, at which point the server state becomesSYN-REVD
. - When the client receives the message, it sends one to the server
ACK=1
It’s acknowledgement. There’s a new oneseq
The serial number isx+1
Here’s another oneack
Make sure the sequence number is previousseq
The serial number+ 1
, i.e.,y+1
. After the completion of the client state programmingESTABLISHED
. - After the server receives the message, the state also becomes
ESTABLISHED
, link channel establishment.
The short summary is:
- Client -> SYN -> server
- Client < -syn +ACK < -server
- 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
- The client sends a message asking to disconnect
(FIN)
. - After receiving the request, the server will first respond to whether it has received the request
(ACK)
. - 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. - The server finishes its work and tells the client it can close the link
(FIN)
. - Client received
FIN
, to handle the things they have done, such as the client has sent to the server did not receiveACK
“, etc. - 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 server
FIN=1
To break the link, and sent oneseq
The serial number=u
At this point the client becomesFIN-WAIT1
. - After the server receives the message, it returns one
ACK=1
Confirmation and moreack
Confirm the serial number. It’s the previous oneseq
The serial number+ 1
, i.e.,u+1
And there’s a new oneseq
The serial number forv
, the server state becomesCLOSE-WAIT
After the client receives the message, the state will change toFIN-WAIT2
. - When the server is ready to close the link, send it to the client
ACK=1
, and also sendack
Confirm the number, go upseq
The serial number+ 1
, i.e.,u+1
And there’s a new oneseq
The serial number forw
I’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 server
ACK=1
The confirmation message was also sentack
Confirm the number, go upseq
The serial number+ 1
, i.e.,w+1
And there’s a new oneseq
The serial number foru+1
Then the state of the client becomesTIME-WAIT
This state will continue2WSL
And if no more messages are received from the back end during the waiting period,2WSL
And then it becomesCLOSED
. After the server receives the message, the state also becomesCLOSED
.
The short summary is:
- Client -> FIN -> Server
- Client < -ack < -server
- Client < -fin < -server
- 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 one
ACK
A packet can reach the server if thisACK
So 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 here2MSL
If a message is retransmitted within that time frame, a response message is given again and it is restarted2MSL
The timer. - The second is in this
2WSL
During the duration of the link, all packets generated during the duration of the link can be erased from the network, so that the newTCP
There 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:
- The client sends to the server
SYN
A request to establish a linkBig brother, can you establish a link?
- The server is going to
ACK+SYN
Package a reply as a messageOld sister, I received your message, can carry on the link, you received?
- The client receives the message and sends it to the server
ACK
As 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:
- The client sends to the server
FIN
Disconnect request(Big brother, my side of things have finished, disconnect the link ~)
- The server receives the message and sends one to the client
ACK
To reply(Sweetie? Disconnect? I see. Please hold on for a minute.
- The server returns when it has finished processing
FIN
To the client(I'm done, please disconnect the link.)
- The client receives the message, finishes its own business, and returns
ACK
To 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.