Establishing TCP requires three handshakes to be established, while disconnecting requires four handshakes. The entire process is as follows:
One of three handshakes is the connection established
Four-time wave to close the connection
11 States of TCP connections
Client Exclusive: (1) syn_sent (2) fin_wait1 (3) fin_wait2 (4) CLOSING (5) time_wait.
Server Exclusive: (1) LISTEN (2) SYN_RCVD (3) close_wait (4) Last_ack.
Total: (1) CLOSED (2) established
State transitions when establishing a connection
Initially, the server and client state are closed before the connection is established. The server starts listening after the socket is created and becomes listen state. The client requests a connection, sends a SYN message to the server, and the client's status changes to Syn_sent. When the server receives a message from the client, it sends an ACK and a SYN message to the client, and the server state changes to SYN_RCVD. The client then receives an ACK, SYN, sends an ACK to the server, the client state becomes established, and the server receives an ACK from the client and becomes established. At this point, 3 handshake completed, connection established!
State transitions when disconnecting
Because TCP connections are full-duplex, disconnecting is a little more hassle than establishing a connection. The client sends a FIN message to the server, requests a disconnection, and its status changes to Fin_wait1. After the server receives the fin, an ACK occurs to the client, and the server state changes to close_wait. When the client receives an ACK, it enters the fin_wait2 state. The connection is now half broken. If the server still has data to send to the client, it will continue to be sent. The FIN message is sent when the server enters the Last_ack state until it is finished. After the client receives the fin from the server, it immediately sends an ACK to the server, when the client enters the TIME_WAIT state and then enters the closed state after a long period of 2MSL. The server receives an ACK from the client and enters the closed state.
At this point, there is another state that is not mentioned: closing state. The closing status indicates that the client has fin, but receives no ACK from the server, but receives the fin from the server. This happens when the server sends an ACK drop, because the network transmission can sometimes be unexpected.
Content Source:
(1) http://blog.csdn.net/whuslei/article/details/6667471
(2) http://blog.csdn.net/engrossment/article/details/8104482
Linux Network programming: Three handshakes and four waves