Detailed explanation of TCP/IP protocol (three-way handshake and four-way wave)

Flag bits in TCP data header:

syn同步标志,用来建立连接
ack确认标志
	syn=1,ack=0请求连接
	syn=1,ack=1接收连接
urg紧急
psh推送,对缓存区刷新
rst复位
fin结束,释放连接

Three handshakes:

(客户端主动连接)	        syn
client(SYNC_SENT)   ---------->    server(LISTEN)
		              syn+ack
client(ESTABLISHED) <----------    server(SYNC_REVD)
		                ack
client(ESTABLISHED) ---------->    server(ESTABLISHED)

Four waves:

(客户端主动关闭)            fin                                               
client(FIN_WAIT_1)    ---------->    server(ESTABLISHED)     
		                  ack                     
client(FIN_WAIT_2)    <----------    server(CLOSE_WAIT)
                        fin+ack      (轮到服务端发送关闭请求)                                              
client(TIME_WAIT)     <----------    server(LAST_ACK)
		                  ack
client(TIME_WAIT)     ---------->    server(CLOSED)                                                         
(等待2MSL超时后客户端变为CLOSED)

Why does TIME_WAIT wait time be 2MSL?
MSL (Maximum Segment LifeTime) is the maximum packet generation time. It is the longest time for any packet to exist on the network. Messages exceeding this time will be discarded. The TIME_WAIT state remains for up to 4 minutes.
Why do we need to maintain the TIME_WAIT state for a period of time after disconnecting?
1. Prevent the last ack from being lost. If the server does not receive the ack after sending the fin, the server will resend it. At this time, if the client socket has been released, it will As a result, no one processes the message and an error is reported.
2. Prevent the previous tcp connection. When there is still data remaining on the network link, create a new tcp connection and wait for processing of the data packets still remaining on the network.

Why does the handshake need three times?
The main reason: to confirm whether the receiving and sending capabilities of both parties are normal, to prevent errors from occurring after the invalid connection request message is delivered to the server (an invalid request refers to a request that the client only sends but does not receive) )
First handshake: The client sends a message to the server
Proving that the client’s sending ability is normal
The second time Handshake: The server receives the message and sends the message to the client
Proving that the server’s receiving and sending capabilities are normal
The third handshake: The client The server sends a message
to prove that the client’s receiving ability is normal

Why does it take four times to wave?
This is caused by TCP's half-close. Half-closed (half-open) means: TCP provides the ability for one party to the connection to receive data from the other end after finishing its sending. In layman's terms, it means that data cannot be sent, but data can still be received.
TCP does not allow data to be transmitted in one direction when the connection is in a semi-closed state (referring to the three-way handshake), so data can only be transmitted after completing the three-way handshake (the third handshake can carry data) .
When the connection is in a semi-closed state, TCP allows one-way data transmission, which means that the server can still send data to the client at this time, and will wait until the server no longer sends data. Send a FIN segment and agree to close the connection now.
This feature is due to the fact that TCP two-way channels are independent of each other, and it also requires a four-way handshake to close the connection.

TCP status:
Handshake:

LISTEN:监听端口
ESTABLISHED:已建立连接
SYN_SENT(SYN_SEND):发送同步请求(请求建立连接),在TCP三次握手期间,主动连接端发送了SYN包后,进入SYN_SENT状态,等待对方的ACK包
SYN_REVD(SYN_RECV):正在确认同步请求,在TCP三次握手期间,被动连接端收到SYN包后,进入SYN_REVD状态,并向对方发送SYN+ACK包

Wave:

CLOSED:已关闭连接
FIN_WAIT_1 	本端主动发送关闭请求,在TCP四次挥手时,主动关闭端发送FIN包后,进入FIN_WAIT_1状态。
FIN_WAIT_2	本端等待接收远端的关闭请求,在TCP四次挥手时,主动关闭端收到ACK包后,进入FIN_WAIT_2状态。
TIME_WAIT	本端已主动关闭连接,等待一段时间(最多为4分钟)该端口才能重新分配,确保服务器正常关闭该连接,在TCP四次挥手时,主动关闭端发送了ACK包之后,进入TIME_WAIT状态,等待最多MSL时间,让被动关闭端收到ACK包。
CLOSING		连接关闭中(连接非正常关闭,等待一段时间后重试),在TCP四次挥手期间,主动关闭端发送了FIN包后,没有收到对应的ACK包,却收到对方的FIN包,此时,进入CLOSING状态。
CLOSE_WAIT	远端等待发送关闭请求或等待超时关闭,在TCP四次挥手期间,被动关闭端收到FIN包后,进入CLOSE_WAIT状态。
LAST_ACK	远端等待接收最后确认关闭请求,在TCP四次挥手时,被动关闭端发送FIN包后,进入LAST_ACK状态,等待对方的ACK包。
主动连接端可能的状态有: CLOSED、SYN_SEND、ESTABLISHED
主动关闭端可能的状态有: FIN_WAIT_1、FIN_WAIT_2、TIME_WAIT
被动连接端可能的状态有: LISTEN、SYN_RECV、ESTABLISHED
被动关闭端可能的状态有: CLOSE_WAIT、LAST_ACK、CLOSED

Socket:
Socket is a class in network programming that stores a quadruple (source ip, source port, target ip, target port) and encapsulates the underlying operations of network connections.
In this way, you only need to create a socket class each time you establish a tcp connection. Establishing a tcp connection can also be said to establish a socket session.
To send a message, you only need to When sending to the other party's socket, reading the other party's message only requires reading the local listening socket.

Linux command to establish a TCP connection with the server:
nc host name or IP address port number

Reference Fee:
https://blog.mimvp.com/article/44678.html
https://blog.csdn.net /jiajiren11/article/details/80887528
https://baijiahao.baidu.com/s?id=1685497690614598021&wfr=spider&for=pc
https //www.pythonheidong.com/blog/article/1230197/d7bdee7dcff396c10fb8/
https://blog.csdn.net/zgy666/article/details/106149576
https://blog.csdn.net/zgy666/article/details/108682078
https://blog.csdn.net/LOOKTOMMER/article/details/121307137< a i=8> https://blog.csdn.net/LOOKTOMMER/article/details/121243476

Guess you like

Origin blog.csdn.net/qq_38022367/article/details/131233803