1. 程式人生 > >TCP三次握手和四次揮手為何只傳送三個包?

TCP三次握手和四次揮手為何只傳送三個包?

首先說一下三次握手過程

一下摘自wiki handshake

Establishing a normal TCP connection requires three separate steps:

  1. The first host (Alice) sends the second host (Bob) a "synchronize" (SYN) message with its own sequence number xx, which Bob receives.
  2. Bob replies with a synchronize-acknowledgment (SYN-ACK) message with its own sequence number y
    y
     and acknowledgement number x+1x+1, which Alice receives.
  3. Alice replies with an acknowledgment (ACK) message with acknowledgement number y+1y+1, which Bob receives and to which he doesn't need to reply.
In this setup, the synchronize messages act as service requests from one server to the other, while the acknowledgement messages return to the requesting server to let it know the message was received.

One of the most important factors of three-way handshake is that, in order to exchange the starting sequence number the two sides plan to use, the client first sends a segment with its own initial sequence number xx, then the server responds by sending a segment with its own sequence number yy and the acknowledgement number x+1

x+1, and finally the client responds by sending a segment with acknowledgement number y+1y+1.

The reason for the client and server not using the default sequence number such as 0 for establishing connection is to protect against two incarnations of the same connection reusing the same sequence number too soon, which means a segment from an earlier incarnation of a connection might interfere with a later incarnation of the connection.

之前一直被直觀誤導,以為第二次握手過程會發送一個syn包和和一個ack包,才發現syn和ack僅僅只是tcp包中的標誌段(。。。)

以下是tcp資料包的格式

理論聯絡實際:Wireshark抓包分析TCP 3次握手、4次揮手過程_13.jpg

所以WIKI中的解釋的(SYN-ACK) message實際是在一個tcp包中將ACK欄位和SYN欄位都置1了

另外附一下標誌各自段的意義:傳送門

還有各個狀態的意義:傳送門

順手附一下知乎上關於為何只有三個包的原因:傳送門