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:
TCP
The purpose of the handshake is to establish a stable link channel. - wave:
TCP
The 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.
seq
The serial number: Identifies the slaveTCP
Byte stream sent from the source to the destination, marked by the initiator when sending data 📌.ack
Make sure the serial number: onlyACK
When the flag is 1, confirm that the serial number field is valid.ack = seq + 1
- Mark:
The SYN (Synchronization)
A:Host
Reach 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:Host
Disconnect the request and the request is completePSH (Push)
A:Host
To anotherHost
Send 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:
- The client sends a message to the server
(SYN)
- The server is ready to connect
- The server is for 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 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 in
CLOSED
The client sends the statusSYN=1
Requests a link to the server and sends oneseq
Sequence number, at which point the client state becomesSYN-SEND
. - The server returns a message
ACK=1
Acknowledge receipt, and confirm ACK number, the previous oneseq
The serial number+ 1
, i.e.,x+1
And this oneseq
The serial numbery
And one moreSYN=1
The link is established, at which point the server state becomesSYN-REVD
. - When the client receives the message, it sends one to the server
ACK=1
Acknowledge receipt and a new oneseq
The serial number isx+1
And one moreack
Make sure the serial number is the previous oneseq
The serial number+ 1
, i.e.,y+1
. After completing the client state programmingESTABLISHED
. - When the server receives the message, the state becomes
ESTABLISHED
The link channel is established.
The brief 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 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
- The client sends a message to disconnect the link
(FIN)
. - After receiving the request, the server responds to whether it received the request
(ACK)
. - 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. - The server is done with its business and tells the client it can close the link
(FIN)
. - The client receives
FIN
To handle things that are done by the client, such as sending something to the server but not receiving itACK
Request, etc. - 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 server
FIN=1
To disconnect the link, and send oneseq
The serial number=u
At this point the client becomesFIN-WAIT1
. - When the server receives the message, it returns one
ACK=1
Confirm the news, andack
Confirm the serial number. It’s the last oneseq
The serial number+ 1
, i.e.,u+1
And a new oneseq
The serial number forv
, the server status changes toCLOSE-WAIT
After the client receives the message, the status becomesFIN-WAIT2
. - Send it to the client when the server has everything ready to close the link
ACK=1
And sendack
Confirm the serial number. Up oneseq
The serial number+ 1
, i.e.,u+1
And a new oneseq
The serial number forw
And 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 server
ACK=1
A confirmation message was sentack
Confirm the serial number. Up oneseq
The serial number+ 1
, i.e.,w+1
And a new oneseq
The serial number foru+1
And then the client state becomesTIME-WAIT
And it will continue2WSL
If no message is received from the back end during the waiting period,2WSL
And then it becomesCLOSED
. After the server receives the message, the status becomesCLOSED
.
The brief summary is:
- Client -> FIN -> Server
- Client < -ack < -server
- Client < -fin <- Server
- 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 one
ACK
The message can reach the server if thisACK
If 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 this2MSL
After receiving retransmitted packets within a period of time, the system sends a reply packet again and restarts2MSL
The timer. - The second is in this
2WSL
Time, you can make all message segments generated during the duration of this link disappear from the network, so that the newTCP
In 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:
- The client sends a message to the server
SYN
Request to establish a link(Big brother, can you establish a link?)
- The server needs to change the
ACK+SYN
Package it as a reply to a message(old sister, I have received your message, you can link, have you received?)
- The client receives the message and sends it to the server
ACK
As 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:
- The client sends a message to the server
FIN
A request to disconnect the link(Eldest brother, I have sent all the things here, please disconnect the link ~)
- The server receives the message and sends one to the client
ACK
To reply(Sister? Want to disconnect? I see. Just a moment, please.
- The server completes what needs to be done and returns
FIN
To the client(I'm done here, please disconnect the link ~)
- The client receives the message, does its own thing, 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 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.