[Linux] four times and waved Detailed three-way handshake

(What is not said, to learn)
recently when learning Linux and found four TCP three-way handshake waving here, especially frequently asked interview, I feel the need to leave a review of the traces, so when you want to see the future where you can find it

A, TCP protocol features

Transmission Control Protocol: connection-oriented, reliable transmission, for throttling

Second, the three-way handshake process

The so-called three-way handshake, refers to establish a TCP connection, the need for the client and the server to send a total of three packets.

Three-way handshake aim is to connect to the server specified port TCP connection establishment, connection and synchronize both the sequence and acknowledgment numbers and information exchange TCP window size. In socket programming, the client performs connect () when the trigger three-way handshake!

  1. The first handshake
    client sends a TCP SYN flag position for 1, indicating that the customer intends to port, as well as initial sequence number X server connections, stored in the header of the sequence number (Sequence Number) field
    Here Insert Picture Description
    2. The second handshake
    the server sends back an acknowledgment packet ACK response, i.e., the SYN flag bit and the ACK flag bit are both 1, while the client is set to the acknowledgment number ISN plus one, i.e., 1 + X-
    Here Insert Picture Description
    3. third handshake
    client sends an acknowledgment packet reconfirmed ACK, SYN flag is 0, ACK flag is 1, and the serial number field +1 ACK is sent to the server, in determining the field to send to each other
    Here Insert Picture Description
    as if this is not the relatively obscure, so I give chestnuts I (because I do not know at the time, suddenly thought of this example to understand the feeling)

In the road side, you see a beautiful woman, you want to say hello to her, then waved to a beautiful woman, greeted her, which can be understood as the first handshake, sending a message. And you see the beauty of waving information to you a smile, by the way also waving to you. This is regarded as the second handshake. Then you feel very happy, but it is uncertain whether people really say hello to you, if you greet someone is behind it, you would not unrequited love? Then again, her smile, and waved once, so you really determined people say hello to you, this is the third handshake.

  • Note that the three-way handshake process, after the server sends SYN-ACK, the client received before AXK TCP connections become mortgage state, while the server is Syn-RECV state, Upon receipt of ACK, the server into ESTABLISH status.

Three or four times and waved process

When the client or server side of any section of hair closes the connection action, wave action will lead
Here Insert Picture Description
to after-because the TCP connection is full duplex, so each direction must be turned off individually, this principle is when the party completes its data transmission characters You can send a FIN to grow the data flow in this direction. However, upon receipt of a FIN packet is not immediately stop the flow of data, the data may continue to flow after receiving the FIN packet, but not continue to send data
(1) A client sends a FIN, used to close the client A data transmission server B
(2) the server B receives the FIN, ACK she played a confirmation message, the acknowledgment number of the received sequence number +1.
(3) closes the server B is connected to the client A, the client sends a FIN packet to A
(. 4) A play client acknowledgment information ACK, acknowledgment and incrementing the received sequence number by 1

Fourth, why the agreement is three-way handshake to establish a connection, and close the connection is four times it?

This issue is also frequently asked interview questions

Because SOKET LISTEN state in the server when the connection request SYN packet is received, it can ** SYN and ACK packet in which a response plays role SYN for synchronization, in which a packet transmission. ** Until then close the connection when the FIN when upon receipt of the request, only that he had no other data can be worn to give you, but you may not have the data has been sent over, so you can not immediately close the connection, otherwise it will cause data loss, you can also send some data to each other again, once again send FIN packet to the other side, you agree to close the connection, so that he will send a confirmation message ACK and FIN packets separately.

Five, TCP reliable transport

How to ensure reliable transmission of TCP:
1, the acknowledgment mechanism - each of the data transmitted ask for confirmation reply
2, the timeout retransmission mechanism - wait for confirmation reply timeout, the retransmission data
3, the protocol field number and acknowledgment number
data there is also a confirmation response effect: the acknowledgment number must be to ensure that data before this number have already received the first data is not received, even if the second received, nor will reply to the second data, because every one must ensure that reply data before this have received
4 protocol checksum field - if the data is not consistent, its data retransmission is required
5, connection-oriented

Published 33 original articles · won praise 13 · views 1062

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/104335666