About those things handshake

origin

Probably studied computer network students know TCP / IP connection is established when the need for three-way handshake, but I do not know whether what must be considered the three-way handshake to establish a connection? You can established by two-way handshake? Why, when they have to disconnect the four-way handshake, rather than using the three-way handshake? In this article we are below one by one answer to these questions.

Message format

Handshake process of establishing a TCP connection, its essence is to connect the two sides reach a formula of transfer rules, and is bound to ultimately reach a common agreed rules. The TCP packet is a TCP connection established consensus. So before you start to understand TCP packet format is necessary. According to Xie Xiren teacher materials, TCP packet format is as follows:

The figure is a lot of fields, but in this case we do not need to understand, which is closely related to these fields below and handshake process, we need to be explained in advance.

  1. Reference: 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. 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. Flag: a total of six, i.e. URG, ACK, PSH, RST, SYN, FIN , and specific meanings are as follows:
    (A) URG: Urgent Pointer (urgent pointer) effective.
    (B) ACK: valid only when ACK = 1 is the acknowledgment number field. When ACK = 0, the acknowledgment number field is invalid
    (C) PSH: When two interactive communication application process, the application may be desired after the end of a command typed by other immediate response. In this case, TCP can be used to push (PSH) operation. After being on the receiving segment PSH = 1, it will be delivered to the receiving application process as soon as possible, and not wait until after the retransmission buffer fills.
    (D) RST: to reset the connection. When RST = 1, indicates the presence of a serious error in the TCP connection, the connection must be released and then re-establish the connection
    (E) SYN: used to synchronize the serial number when establishing a connection.
    (F.) FIN: Used to release a connection.

    Three-way handshake

    The so-called three-way handshake refers to the TCP connections require the client and the server to send a total of three packets to establish a connection confirmation at the time of the establishment. Entire process is as follows:

operating effect
The first handshake The client SYN flag bit is set to 1, a randomly generated value seq = x, and the packet is sent to the server, it enters into the customer side SYN_SENT state, the server waits for acknowledgment. The client received a letter server, then the server will understand the client's own signaling capability and the ability to accept there is no problem
The second handshake After the packet is received by the server flag SYN = 1 know the client requests to establish a connection, the server SYN and ACK flag bit are set to 1, ack = x + 1, generating a random value seq = y, and the data packet sent to the client to confirm the connection request, the server enters a state SYN_RCVD Server client receives the letter, then the client will understand the signaling capability and capacity of the receiving server is no problem, then the server does not know how his receiving ability signaling capability and the client, so the introduction of the third handshake
Third handshake 客户端收到确认后,检查ack是否为J+1,ACK是否为1,如果正确则将标志位ACK置为1,ack=y+1,并将该数据包发送给服务器,服务器检查ack是否为y+1,ACK是否为1,如果正确则连接建立成功,客户端和服务器进入ESTABLISHED状态,完成三次握手,随后客户端与服务器之间可以开始传输数据了。 客户端发信服务器收到信之后,就会明白自己的发信能力和客户端的收信能力都没有问题,两个都会明白彼此之间收发能力都正常。可以进行数据传输 。

到这里可能会有人问了,两次握手之后客户端直接向服务器端直接发送数据,不也可以吗?
严格意义上讲,第二次握手之后,客户端直接发送数据也可以向服务器确定客户端的接受能力和自己的发信能力。但是直接发送的话,可能由于服务器端没有准备就绪,从而造成数据包的丢失,也就是说第三次的握手在一定程度上是提前让服务器做好接受数据的准备。

挥手道别

所谓四次握手就是指终止TCP连接的时候,需要客户端和服务器总共发送4个数据包以确认连接的断开。整个过程如下图所示:

操作 作用
第一次挥手 客户端发送一个FIN,用来关闭客户端到服务器的数据传送,客户端进入FIN_WAIT_1状态。 客户端发信,服务器端到收信。客户端告知服务器端自己需要发送的数据已经发完,但服务器端不一定全部收到
第二次挥手 服务器收到FIN后,发送一个ACK给客户端,确认序号为收到序号+1(与SYN相同,一个FIN占用一个序号),服务器进入CLOSE_WAIT状态。 服务器端发信,客户端收信。服务器端告知客户端其发送的数据自己已经全部收到了。但是服务器端不一定数据发送完毕
第三次挥手 服务器发送一个FIN,用来关闭服务器到客户端的数据传送,服务器进入LAST_ACK状态。 服务器端发信,客户端收信。服务器端告知客户端自己需要发送的数据以及发送完。但是客户端不一定收到了所有数据
第四次挥手 客户端收到FIN后,进入TIME_WAIT状态,接着发送一个ACK给服务器端,确认序号为收到序号+1,服务器端进入CLOSED状态,完成四次挥手。 客户端发信,服务器端收信。告知服务器端其发送的数据自己已经全部接收到了,双方可以释放连接了

到这里可能大家又有疑问了,为什么TCP在建立连接的时候需要三次握手就可以了,而分别的时候却要挥手四次?
这是因为服务端在LISTEN状态下,收到建立连接请求的SYN报文后,把ACK和SYN放在一个报文里发送给客户端。而关闭连接时,当收到对方的FIN报文时,仅仅表示对方不再发送数据了但是还能接收数据,己方也未必全部数据都发送给对方了,所以己方可以立即close,也可以发送一些数据给对方后,再发送FIN报文给对方来表示同意现在关闭连接,因此,己方ACK和FIN一般都会分开发送。

小结

到这里可能大家就明白了,TCP在建立连接时要进行三次主要是为了让双方都明确自身和对方收发能力正常。而TCP在断开是进行四次挥手,主要是为了保证收发双方数据都发送完毕,并且对方都完全接收到。

Guess you like

Origin www.cnblogs.com/goWithHappy/p/three_time_handshake.html