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 :
name | concept | Number of lines |
---|---|---|
Simplex | At any one moment, if the data can only be sent in one direction | Only 1 (=1) |
Half duplex | At a certain moment, data can be transmitted in one direction, or in the opposite direction to the other, and alternately | At least 1 (>= 1) |
Full duplex | Data can be sent and received in both directions at any time | More 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 fromTCP
byte sent by the source to the destination stream, this time labeled initiator 📌 transmission data. -
ack
Determine the serial number : Only when theACK
flag is 1, the confirmation serial number field is valid,ack = seq + 1
flag bit:
-
SYN(Synchronization)
: OneHost
actively initiates a connection to anotherHost
, requesting synchronization. -
ACK(Acknowledgement)
: Because of the connection and reliability constraints, theTCP
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)
: aHost
active disconnect request, the request is completed -
PSH(Push)
: aHost
to anotherHost
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
(SYN)
message to the server 060ab4f018ebf9- The server is ready to connect
(SYN)
gives a(ACK)
client's 060ab4f018ec45- The server sends a
(SYN)
to the client - Client is ready
- 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:
- At the beginning, both the client and the server are in
CLOSED
. The client sendsSYN=1
to the server to request to establish a link and sends aseq
serial number. At this time, the state of the client becomesSYN-SEND
. - The server receives the message and returns a
ACK=1
indicate receipt, and ack confirms the serial number, which is the previousseq
serial number+1
, which isx+1
, and the currentseq
serial numbery
, and there is aSYN=1
establish a link. It becomesSYN-REVD
. - After receiving the message, the client sends a
ACK=1
to the server to confirm receipt, and a newseq
serial number isx+1
, and anotherack
that the serial number is the previousseq
serial number+1
, that is,y+1
. After the completion of the client state programmingESTABLISHED
. - After the server receives the message, the status also becomes
ESTABLISHED
, and the link channel is established.
The brief summary is:
- Client -> SYN -> Server
- Client <- SYN+ACK <- Server
- 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
(FIN)
message to request to disconnect the link 060ab4f018f0a8.- After the server receives the request, it will first respond with
(ACK)
whether it has received the request. - 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. - The server finishes processing its own affairs and tells the client to close the link
(FIN)
. - The client receives
FIN
and handles the things it has done. For example, the client sends a request to the server without receivingACK
. - 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
- At the beginning, the client sends
FIN=1
to the server to disconnect, and sends aseq
serial number=u
. At this time, the client becomesFIN-WAIT1
. - After the server receives the message, it returns a
ACK=1
confirmation message, andack
confirmation sequence number, which is the previousseq
sequence number+1
, that is,u+1
, and a newseq
sequence number isv
, and the status of the server is nowCLOSE-WAIT
. After receiving the message, the status will change toFIN-WAIT2
. - When the server is ready to close the link, send
ACK=1
to the client, and sendack
confirm the serial number, the lastseq
serial number+1
, that is,u+1
, and a newseq
serial numberw
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 becomesLASE-ACK
. - After the client receives the message, it sends the
ACK=1
confirmation message to the server, and also sends theack
confirmation serial number, the lastseq
serial number+1
, that is,w+1
, and a newseq
customer serial number isu+1
, and then The status of the terminal becomesTIME-WAIT
, which will continue to2WSL
. If no back-end messages are received during the waiting period,2WSL
will becomeCLOSED
. 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
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 thisACK
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 client2MSL
time period, it will give a response message again and restart the2MSL
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 newTCP
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:
SYN
to the server to establish a link request(Big brother, can you establish a link?)
- The server
ACK+SYN
package 060ab4f018fa79 as a message reply(Old girl, I have received your message, you can link, have you received it?)
- 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:
- The client sends a
FIN
disconnect request to the server(Brother, I have finished sending everything here, disconnect the link~)
- Server receives information, the client sends a
ACK
reply(old Meier? To break the link? I know you'll wait Son)
- 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~)
- 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.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。