Network: TCP 11 states three-way handshake four-way handshake

Reprinted from : http://www.imekaku.com/2016/04/24/tcp-11-status/

Thank you blogger for sharing!

11 state transition diagrams of TCP

tcp connection state transition

TCP connection packet switching diagram


state transition process

The server side creates a socket socket, binds bind, listens to listen, and the socket is passively opened.

accept blocks until a socket arrives.

 

The client creates a socket socket and connects to the server.

Blocks until connected to the server (but returns an error if the server is not open)

 

  • three-way handshake

The client actively opens the state to become SYN_SENT, and sends a SYN section to the server,

The server accepts the SYN segment status as SYN_RCVD, and sends a SYN segment and ACK confirmation message to the client.

The client accepts a SYN section and an ACK confirmation message, the state changes to ESTABLISHED, and an ACK confirmation message is sent to the server.

The service receives the ACK confirmation message and the status becomes ESTABLISHED.

This allows the server and client to exchange data.

  • four-way handshake

The client side calls close, actively closes, the status changes to FIN_WAIT_1, and sends a FIN section to the server,

The server accepts the FIN section, the state changes to CLOSE_WAIT, and sends an ACK confirmation message to the client,

The client accepts the ACK confirmation and the status changes to FIN_WATI_2.

The server calls close, sends a FIN section to the client, and the status changes to LAST_ACK,

The client accepts the FIN section, the status changes to TIME_WAIT, and sends an ACK confirmation message to the server,

The server accepts the ACK confirmation message, and the status changes to CLOSED.

The client enters the CLOSED state after 2 times MSL (maximum segment lifetime).

CLOSING status

As shown in the TCP state transition diagram.

If the client sends a FIN segment to the server, it becomes FIN_WAIT_1 state,

Then the FIN segment is received, and an ACK confirmation message is sent to the server, and the client state changes to the CLOSING state.

At this time, the customer service receives the ACK confirmation message of the FIN sent by itself, and the state changes to the TIME_WAIT state.

After 2 times MSL time, the state changes to CLOSED state.

  • what happens

The client calls close to send the FIN segment to the server, and the server calls close to send the FIN segment to the client.

TIME_WAIT status

The TIME_WAIT state exists for two reasons:

  1. Reliable termination of TCP full-duplex connections
  2. Allow old duplicate sections to die in the network

 

  • first reason

When the server calls close to close the socket, the state changes to LAST_ACK and sends a FIN section to the client,

The client accepts the FIN segment, the state changes to the TIME_WAIT state, and an ACK confirmation message is sent to the server.

At this time, the ACK confirmation information is lost.

The server does not receive an ACK, it will resend its final FIN,

So the client must maintain state information to allow the client to resend the final ACK.

If the client does not maintain state information, it will respond with an RST section that will be interpreted by the server as an error.

  • second reason

It is necessary to ensure that each time a TCP connection is successfully established, the old, duplicate packets from the previous incarnation of the connection have been lost on the network.

That is: if the previous connection is disconnected, we establish a new connection between the same IP address and port as before,

Newly established connections are called incarnations of previous connections because they have the same IP address and port number.

TCP must prevent old duplicate packets from a connection from reappearing after the connection has terminated,

and thus be misinterpreted as an incarnation of the same connection.

The duration of TIME_WAIT is 2 times MSL, so the packet with the most surviving MSL in one direction will be discarded.

At the same time, the reply in the other direction also survives at most MSL and will also be discarded.


Guess you like

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