Review three handshakes and four waves

One, three handshakes:

The first handshake: When the connection is established, the client sends a syn packet (syn=j) to the server, and enters the SYN_SENT state, waiting for the server to confirm; SYN: Synchronize Sequence Numbers.

The second handshake: The server receives the syn packet and must confirm the client's SYN (ack=j+1), and at the same time sends a SYN packet (syn=k), that is, the SYN+ACK packet, at this time the server enters the SYN_RECV state;

The third handshake: the client receives the SYN+ACK packet from the server, and sends the confirmation packet ACK (ack=k+1) to the server. After the packet is sent, the client and the server enter the ESTABLISHED (TCP connection is successful) state, which is completed three times. shake hands.

After completing the three-way handshake, the client and the server begin to transmit data. In the above process, there are some important concepts:

Unconnected queue
In the three-way handshake protocol, the server maintains an unconnected queue, which opens an entry for each client's SYN packet (syn=j), which indicates that the server has received the SYN packet and sends an acknowledgment to the client.
Awaiting confirmation package from customer. The connection identified by these entries is in the SYN_RECV state on the server. When the server receives the confirmation packet from the client, the entry is deleted and the server enters the ESTABLISHED state.


Two, four waved:

Since the TCP connection is full-duplex, each direction must be closed separately. This principle is that when one party completes the data transmission task, it sends a FIN to terminate the connection in this direction. Receiving a FIN only means that There is no data flow in this direction, that is, no more data will be received, but data can still be sent on this TCP connection until FIN is also sent in this direction. The side that shuts down first will perform an active shutdown, while the other side will perform a passive shutdown, as depicted in the diagram above.

The first wave: The
client sends a FIN to close the data transfer from the client to the server, and the client enters the FIN_WAIT_1 state.
The second wave:
After the server receives the FIN, it sends an ACK to the client to confirm that the serial number is the received serial number + 1 (same as SYN, one FIN occupies one serial number), and the server enters the CLOSE_WAIT state.
The third wave: The
server sends a FIN to close the data transfer from the server to the client, and the server enters the LAST_ACK state.
The fourth wave:
After the client receives the FIN, the client enters the TIME_WAIT state, and then sends an ACK to the server, confirming that the serial number is the received serial number + 1, the server enters the CLOSED state, and completes the four waves.

Third, why is TCP a three-way handshake?
Because three times is the minimum value to ensure that the two parties can clearly understand each other that the other party can receive and send.

Fourth, why does TCP wave four times?
Summary of my own words: After A sends a fin, B may have data that has not been sent. So B can't send ACK and fin together. To be developed separately, so you need to send 4 times.
The following is the standard answer: This is because the server in the LISTEN state, after receiving the SYN message for the connection establishment request, sends the ACK and SYN in one message to the client. When closing the connection, when receiving the FIN message from the other party, it only means that the other party no longer sends data but can still receive data, and not all data is sent to the other party, so the party can immediately close or send some data. After the data is sent to the other party, the FIN message is sent to the other party to express the agreement to close the connection now. Therefore, the own ACK and FIN are generally sent separately.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325366712&siteId=291194637