[TOC]
Preface
- TCP is an end-to-end protocol;
- TCP is a stateless protocol;
One, TCP's three-way handshake-connection
1.1 Graphical process
- SYN, the synchronization flag, is used to synchronize its own serial number to the other party when establishing a connection.
- ACK, the acknowledgement flag bit, when ack (acknowledgement sequence number) needs to be sent, it must be set to 1.
- seq, data sequence number, table name, what is the sequence number of the first data sent in this request, it is self-defined for the first time.
- ack, the serial number of the next data, tell the other party what the serial number of the first data that should be sent next time.
- established, the connection has been established.
The TCP three-way handshake is essentially the process of maintaining the seq (data sequence number) of both parties.
note:
- The SYN segment does not carry data, but it consumes a sequence number;
- The ACK segment can carry data. If it is not carried, the next sequence number will not change, so after the last handshake, the next connection sequence number still starts from K+1;
diagram :
- A calls B, this is a synchronous connection, this time the data starts from J;
- B received;
- B calls A and receives a synchronous connection. Next time, please start sending data from J+1, and synchronization is also required. This time the data starts from K;
- A receives it and establishes a connection;
- A calls B and receives a synchronous connection, then please start sending data from K next time;
1.2 Why can't it be a four-way handshake?
In fact, it can be seen that the second time of the handshake can be divided into two parts to complete, send ACK first, and synchronize SYN, so that it is a four-way handshake. why not? Because the effect is the same, there is no need~. The same is not true for five times, six times, seven times and so on.
1.3 Why can't it be two handshake?
This is mainly to prevent expired connection request segment suddenly transferred to the other, resulting in an error.
Invalid connection request message segment: first handshake did not reach the other party due to network congestion or other reasons.
If it is two handshake, what will happen wrong?
In the case of two handshake, the establishment (connection establishment) of after the first handshake.
- If A sends a connection request for the first time (seq=J), it arrives at B because of network congestion, and then after sending a second connection request, it arrives successfully, the two parties subsequently connect and continue to send data (A sends to B's seq has reached N).
- At this time, the first connection request arrives at B, and B thinks that A needs to re-establish the connection (because TCP is stateless, so it can only be considered mechanically), then B establishes the connection, sends the confirmation and synchronization request to A, and waits to receive A The data whose serial number is J+1.
- A receives the confirmation and synchronization request from B, but A is already connected to B at this time, so it ignores this confirmation request and continues to send data with a serial number of N+1.
- B receives data with a sequence number of N+1, but not data with J+1, and does not receive it.
- A did not wait for B's reply, and kept retransmitting after a timeout. After several times, it reported an error and waited for reconnection.
Why does the three-way handshake fail to " failed connection request segment suddenly transmitted to the other party, resulting in an error"?
Because B establishes a connection after the third handshake of , the suddenly transmitted to the other party, and it will not change the sequence number of the data that B receives next time A (the main for the failure of the two handshake Reason: Next time B receives A's data sequence number from N+1 to J+1). So when A continues to send N+1 serial number data after A ignores it, B can receive it normally.
After the second or third handshake is congested, what will happen if it is suddenly transmitted back to the opponent's hands?
Because both the second and third handshake needs to have prospects, that is, the first handshake (the prospect of the second handshake) and the first and second handshake (the prospect of the third handshake), because there is no prospect, so naturally Will be ignored.
2. Four Waves of TCP-Release
2.1 Diagram
- FIN, the connection end flag, indicates that the sender requests to disconnect.
- MSL, the longest message segment life, is about 2 minutes. As the times progress, it continues to become shorter.
- 2MSL, 2 MSL time, is the duration of a waiting timer.
note:
- When the FIN flag is 1, even if the request does not carry data, a serial number will be consumed.
diagram :
- A wants to close the connection and handle the afterwards of closing the connection;
- A calls B, this is a close request, and the sequence number is u;
- B receives A's close request, and at the same time reports to the application to close the connection;
- B calls A. This is an acknowledgment request. This time the data sequence number is v, and the next data transmission starts from u+1 (this indicates that B received A's last (u) request);
- B handles the closing of the connection, and at the same time sends the data that needs to be sent (the data sent at this time must be kept ack=u+1, because it will be used for subsequent A sending), after sending it, it will close the operation;
- B calls A, this is a close request, this sequence number is w, and the next data transmission starts from u+1;
- A calls B. This is an acknowledgment request. The sequence number is u+1, and the next data transmission starts from w+1;
- After A waits for 2MSL, close the connection;
2.2 Why does A need to wait for 2MSL at the end?
- Ensure that the last message segment sent by A to B can reach B. If B cannot be reached, within this 2MSL time period, it is completely enough for B to retransmit the FIN segment over time, and A allows it to be received. If A receives it, it will continue to send the last wave and reset the waiting timer. There are also times to wait for the timer to reset. If it is reset multiple times, A will be turned off directly.
- Prevent the "failed connection request segment" from appearing in the wave phase. In this 2MSL time period, it is enough for B to receive the previous message segment that did not arrive due to network congestion. Of course, the message segment has a valid duration. If the valid duration is exceeded, it will be discarded. In this way, an error will not be generated because the message segment of the last connection is received when the connection is established next time.
2.3 What should I do if A powers down and exits?
TCP has a keep-alive timer . When both parties receive each other's message segment, the keep-alive timer will be reset. If the keep-alive timer expires, it will actively close the TCP connection.
Three, summary
The entire TCP connection, sending data to each other (you send data to me, and I reply you to receive it), and the wave of maintaining the serial number 160cf58031eee1. This serial number is the unique identifier of the sentence I said to the other party.
Both seq and ack are particularly important. seq represents the uniqueness of a party's data; ack not only tells the other party where to send it next time, but also verifies whether the data sent by the other party is correct.
The entire communication is for both parties, and no one party is the leader of the data.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。