Home » Linux » TCP four handshake (close connection)

TCP four handshake (close connection)

reference address: http://www.2cto.com/net/201208/149565.html

client or server can actively launch waving action, in socket programming, either side of the close () operation, you can generate wave operation.

because your TCP connection is full duplex, so each direction must be individually closed. This principle is connected when a party sends data and it can send a FIN to terminate the direction. It receives a FIN only means no data flow in this direction, a TCP connection can send data to receive a FIN. First off the party will perform active off, while the other side performing passive off.

waving for the first time:

TCP client sends a FIN to turn off data transfers from the client to the server.

second times waving:

the server receives this FIN, which sends back a ACK, confirming that the serial number is the serial number received, plus 1.

third times waving:

the server closes the client connection and sends a FIN to the client.

Fourth Times waving:

the client sends back the ACK message confirmation and sets the confirmation serial number to receive the serial number plus 1.

several states:

FIN_WAIT_1: after the active party sends the FIN packet, enters the condition, waits for the other party to return the ACK message.

FIN_WAIT_2: that is to receive the other party's ACK package, but the other party also sent data packets, so wait to close the connection.

TIME_WAIT: indicates the status of CLOSEED after receiving the FIN packet of the other party and sending a ACK message while waiting for the 2MSL to be cut back to the available state of the.

CLOSING: this state is more special, relatively rare, that is, after sending the FIN message, did not receive the other party's ACK package, but first received the other party's FIN package, the most typical example is that both sides at the same time to close the FIN message.

CLOSE_WAIT: indicates waiting to be closed. That is, if you do not have data to send, that is to turn off the connection, that is, to send the FIN message.

LAST_ACK: after the passive party sends the FIN message, it waits for the ACK message ".
" finally