TCP connection is established, Disconnect

Reference blog: https://www.cnblogs.com/LCCRNblog/p/5228648.html

TCP packet format:

 

 Important fields:

(1) NO: Seq number, representing 32 bits, used to identify the TCP byte stream from the source to the destination of transmission, the initiator sends data of this labeling.
(2) the acknowledgment number: Ack number, accounting for 32, when only the ACK flag bit is 1, the acknowledgment number field is valid, Ack = Seq + 1.
(3) Flags: a total of six, i.e. URG, ACK, PSH, RST, SYN, FIN , and specific meanings are as follows:
(A) the URG: Urgent Pointer (urgent pointer) effective.
(B) ACK: acknowledgment number is valid.
(C) PSH: recipient should be the message to the application layer as soon as possible.
(D) RST: reset the connection.
(E) SYN: initiate a new connection.
(F) FIN: releasing a connection.

 

Ack acknowledgment number of the ACK flag - validator initiator Ack = Req + 1, both ends of the pair; represents a capitalization flag ACK packets.

Establish a connection (three-way handshake):

 

 When establishing a TCP connection, the client and the server needs to send a total of three packages to confirm the establishment of the connection.

 

 

(1)第一次握手:Client将标志位SYN置为1,随机产生一个值seq=J,并将该数据包发送给Server,Client进入SYN_SENT状态,等待Server确认。
(2)第二次握手:Server收到数据包后由标志位SYN=1知道Client请求建立连接,Server将标志位SYN和ACK都置为1,ack=J+1,随机产生一个值seq=K,并将该数据包发送给Client以确认连接请求,Server进入SYN_RCVD状态。
(3)第三次握手:Client收到确认后,检查ack是否为J+1,ACK是否为1,如果正确则将标志位ACK置为1,ack=K+1,并将该数据包发送给Server,Server检查ack是否为K+1,ACK是否为1,如果正确则连接建立成功,Client和Server进入ESTABLISHED状态,完成三次握手,随后Client与Server之间可以开始传输数据了。

三次握手的序列号和确认号,如果用(序列号,确认号)表示一次握手,则三次握手的过程序列号和确认号如下:

1) 第1步:客户端向服务器发送一个同步数据包请求建立连接,该数据包中,初始序列号(ISN)是客户端随机产生的一个值,确认号是0;
2) 第2步:服务器收到这个同步请求数据包后,会对客户端进行一个同步确认。这个数据包中,序列号(ISN)是服务器随机产生的一个值,确认号是客户端的初始序列号+1;
3) 第3步:客户端收到这个同步确认数据包后,再对服务器进行一个确认。该数据包中,序列号是上一个同步请求数据包中的确认号值,确认号是服务器的初始序列号+1。

1、(X,0)

2、(Y,X+1)

3、(X+1,Y+1)

 

断开连接(四次挥手):

中断连接端可以是Client端,也可以是Server端。

(1)第一次挥手:Client发送一个FIN,用来关闭Client到Server的数据传送,Client进入FIN_WAIT_1状态。
(2)第二次挥手:Server收到FIN后,发送一个ACK给Client,确认序号为收到序号+1(与SYN相同,一个FIN占用一个序号),Server进入CLOSE_WAIT状态。
(3)第三次挥手:Server发送一个FIN,用来关闭Server到Client的数据传送,Server进入LAST_ACK状态。
(4)第四次挥手:Client收到FIN后,Client进入TIME_WAIT状态,接着发送一个ACK给Server,确认序号为收到序号+1,Server进入CLOSED状态,完成四次挥手。

四次挥手的序列号和确认号

(u,0)

(v,u+1)

(w,u+1)

(u+1,w+1)

Guess you like

Origin www.cnblogs.com/Hqx-curiosity/p/12161629.html