TCP protocol establishes a connection with the release of the connection

TCP protocol establishes a connection with the release of the connection

TCP protocol is a connection-oriented protocol to transfer data, you need to establish a connection, transfer data, the connection is released in three stages. Which requires three-way handshake to establish a connection, and the connection needs to release four-way handshake, the principle is as follows:

establish connection:

First, the server-side to open the service, create the TCB (Transmission Control Module), then enter the server LISTEN state, waiting for client connections.

The client also needs to establish the TCB , and then sends a request to the server: the SYN =. 1 , SEQ = W, then enter SYN-SEND state, waiting for a response.

After the server receives the request, transmits: the SYN =. 1 , the ACK =. 1 , ACK = + W. 1, SEQ = U, then enter SYN-REVD state.

After receiving the transmission: the ACK =. 1 , SEQ. 1 = W +, U +. 1 = ACK , into the ESTABLISHED stage.

After the server receives, enter ESTABLISHED stage, TCP connection formally established.

Three-way handshake reason

If A sent to B datagram way delay, A retransmits, B to receive a second, smooth with A to establish a connection. AB after completing transmission of data releases the connection, then B has received a first, that A and also connected, then responds to this is if the two connector B has the A connection is established , this time B has etc. A transmission data, consumption of resources. It must be three-way handshake.

Connection release

Client makes a request, the connection release request, transmits: the FIN =. 1 , SEQ = W, then enter FIN-WAIT-1 stage.

The server receiving the request, transmitting :( . 1 ) the ACK. 1 =, = W + ACK. 1, SEQ = U, into the CLOSE-WAIT stage. At this time, if the server is still required to send data, it continues to send. Until no data is sent, the client sends :( 2 ) the FIN =. 1, the ACK. 1 =, = W + ACK. 1, SEQ = Y, into the LAST-ACK stage.

The client receives ( 1 ), enters the FIN-WAIT-2 stage, receive ( 2 ), transmitted to the server: the ACK = 1 , ACK = Y + 1, SEQ 1 = W +, into the TIME-WAIT stage, the client wait two the MSL (maximum segment lifetime), close the connection.

After the server receives, it closes the connection.

TIME-WAIT role

1. If B does not receive A confirmation request transmitted last, the B will re-send the FIN , A in 2MSL time receive B retransmission, A can then be confirmed, thus preventing B if it receives no acknowledgment, the server end can not correctly enter CLOSED.

2. In 2MSL in all the network connections associated with the current packet is consumed, it has failed to prevent problems segments connection request.

Keep-alive timer

There is a server-side keep-alive timer for recording client state, so as to avoid waiting in vain, every time the client receives the message of keep-alive timer is updated, usually two hours, if more than two hours have not received the new, the server send probe packets to detect the status of the client.

 

 

Published 47 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/nanchengyu/article/details/53365996