TCP connection establishment and release

TCP is a connection-oriented protocol, which is divided into a data transmission connection is established, data transfer, the connection is released in three stages.

0 to establish a connection

Establishment of the connection is often said that the "three-way handshake":

  • The client sends to the server a SYN packet (SYN = 1 indicates that this is connected to a connection request or receive), and a randomly selected starting sequence number x;
  • Server responds with a SYN packet, and ACK (acknowledge bit) 1 [ACK = TCP segment header of the ACK (acknowledgment number field) is valid 1], randomly selected starting serial number a server-side y, and ack field is set to x + 1, represents the client has received a SYN packet sent, received expected next sequence number x + 1 of the packet;
  • Client a response ACK message to the sequence number x + 1, y + and ack field is set to 1, it indicates the server has received SYN packet, received expected sequence number y + 1 of the next packet.
    Note: The serial number seq, also known as ISN (Initial Sequence Number)
    Pictures from Baidu
    reason for the use of three-way handshake instead of twice or four times, from the handshake mechanism for the purpose of talking about:
    Handshake mainly to confirm the sending of both parties, reception is normal , SEQ ID incidentally initialization, ready for subsequent data transfer, so that:
    the first handshake: a client server receives network packets sent, the server will understand receiving capability of the transmission capabilities of the client, the server normally;
    second handshake: server contract, the client receives, the client will understand the sending server, receiving, sending client receives is normal;
    after the second handshake, the client'd all understand, but the server can not confirm the client can normally received, the server is in the normal , so the two-way handshake is not enough;
    third handshake: server receives network packets sent by the client, the server sends the last confirmed their normal client receives normal, so four times handshake is redundant.

    1 supplementary question

  • Sequence number (ISN) The reason why the random generator, in order to avoid being attacked (if fixed, ack is transmitted at once is obvious);
  • The first two-way handshake can not carry data, and the third can carry. The first handshake unknown server receives the ability, if carrying data may be lost. The second client receives the capability is unknown, data may be lost if the carry. The third client knows the server receives normal, and they have in ESTABLISHED state;
  • After the server sends a SYN packet, in the SYN-RCVD state, then a different connection request will be placed half-connection queues , the three-way handshake is completed after the connection request is placed in a full connection queue inside.

2 connection release

Process connection release is often said that the "four wave":
PS figures to client initiates a release request, for example

  1. The client sends a release request, the FIN (stop bit) is set to 1, it indicates that the client send data has been completed, the release request;
  2. Server sends an ACK packet, ack acknowledgment number is u + 1, this time from the client to the server connection is released, in a semi-closed state ;
  3. If the server is also disconnected, it transmits a connection release message (FIN = 1) to the client;
  4. The client sends a response packet, the sequence number seq of u + 1, wait for a period of time (2MSL) ensure that the server receives the ACK packet, then close the connection.

Pictures from Baidu
The reason why the client to wait 2MSL time to shut down for two reasons:

  • Once the server has not received ACK packet, the server will resend FIN message, the client receives FIN message again, you know ACK packet before sending the loss, the timer will reset the time to wait for the ACK packet and retransmits 2MSL Wen.
    Without this waiting period, if the server does not normally receive ACK packet, then retransmitted FIN segment will not reach the client, the server can not shut down properly.
  • The client finished his last ACK packet, after 2MSL, in this connection generated message will disappear from the network, the next time to avoid this new old connection request segments connected appears.

Guess you like

Origin www.cnblogs.com/EIMadrigal/p/11560205.html