The TCP protocol is very important, and here it's connected and released to tidy up.
The first is a three-time handshake:
1, the client initiated, like the server sent the message syn=1,ack=0, and then selected an initial sequence number: Seq=x.
SYN What do you use it for?
Create a synchronization sequence number when linking, indicating that this is a message segment of the connection request when the Syn=1 is ack=0 at the same time. If the other party intends to link, the returned message inside Syn=1,ack=1,. In this sense, syn=1 indicates that this is a ' request ' or ' Accept request ' message.
The Syn=1 message segment cannot carry data. But to consume a sequence number,
ACK What do you use it for?
It is only valid to confirm the font size (the number of the first data byte expected to receive the next segment of the other message) only when ack=1. Therefore, TCP provides that when a link is established, all incoming messages should have an ACK of 1 (in fact, only the ACK of the client-initiated link request message is not set to 1).
Current status: Client enters Syn-send State;
2. After receiving the SYN=1,ACK=0 request message, the server returns a syn=1,ack=1 acknowledgment message.
At the same time, the confirmation number ack=x+1, also chooses an initial serial number for oneself seq=y
Current status: Server enters SYN-REVD State;
3, the client received the return information of the server, but also to the server to return the last confirmation, ack=1, confirmation number ack=y+1;
Current status: client enters established state.
Here's why two handshake is not possible, it has to be three times:
first of all, a normal situation is that the client sent a request to link the message, but because of network reasons lost, it is impossible to receive server-side confirmation. At this time, the client only once again send the original request message, this time the server received a confirmation, the client reconfirm, the link is established.
then consider an abnormal situation , the client sent two requests to link the message, the second is captured by the server, return data, completed two handshake. After the data transfer is complete, the link closes. But at this time, the first congestion request message now reached the server side, the server also thought that the client to establish a connection again, so send confirmation, and then open themselves, waiting for the client to send over the data. As a result, a lot of network resources are wasted.
If the three-time handshake, the server received an expired request message, return confirmation information, the client received the information of the server was baffled, thought: I did not want the link, you return this is not crazy. So pail a word. The server has not received the third handshake data for some time, knowing that the client did not request to establish a link, tears left.
Then there are four breakup:
Now both sides are in established state.
1. The client initiates a request to disconnect the link. Fin=1,seq=u. U is the ordinal of the last byte transmitted before +1.
FIN: Used to release a link, when Fin=1, indicates that the sender of this message has completed the data sent, no new data to be transmitted, and requested to release the link.
The client enters the fin-wait-1 state, waits for the server to return the acknowledgement;
2. After the server receives the client's request to disconnect the message, it returns a confirmation message. Ack=1,seq=v,ack=u+1.
The server enters the close-wait state.
At this time, the client can not send messages to the server, can only receive. But if the server still has information to pass to the server, it can still be transmitted.
3, when the server also does not have the information can be transmitted to the client to send a request to end the message. Fin=1,ack=1,
Ack=u+1,seq=w.
State at this time: the server enters the Last-ack state.
4, the client receives the FIN=1 message, returns the confirmation message, ack=1,seq=u+1,ack=w+1.
After sending, the client enters a wait state and waits two time periods. Shut down.
Why wait for two time periods at the end of the day?
1, the client's last ACK message is lost at the time of transmission, the server did not receive this message. This waiting.
The server will time out to retransmit the fin message, and then the client will return to the last ACK message, waiting for two time periods to complete the shutdown. If you do not wait for these two time periods, the server retransmission message will not be received. The server cannot shut down gracefully because it cannot receive information from the client.
2, prevent the last time in the three handshake mentioned in the failure of the message interference. After two time periods, all messages will disappear in the network, ensuring that the next reconnection will have a messy message impact.
TCP Love History: Three handshake and four breakup