Java interview knowledge (fifty-nine) TCP three-way handshake and four waving agreement

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_33945246/article/details/97290177

Here Insert Picture Description

Three-way handshake

Three-way handshake: (I want you to build links, you really want me to link it, and I really want you to build links, successful)

  • The first handshake: a client sends syn packets (syn = x) to the server, and enters SYN_SEND state, waiting for the server to confirm;
  • Second handshake: server receives syn packets, must confirm the customer SYN (ack = x + 1), while themselves sends a SYN packet (syn = y), i.e., SYN + ACK packet, then the server enters a state SYN_RECV;
  • Third handshake: the client receives the SYN + ACK packet to the server, the server sends an acknowledgment packet ACK (ack = y + 1), this packet is sent, the client and server into the ESTABLISHED state, complete the three-way handshake.

Bag handshake does not include the transfer of data, after three-way handshake is completed, the client and server before the official start transferring data. Ideally, TCP connection, once established, before any party in the communication of the two parties take the initiative to close the connection, TCP connection will be kept down.


Four waving

Four waved :( I want you break the link; good, I have to break it and you break the link; good, breaking it). :

  • The first wave: the client sends a proactive closed FIN, for closing the client to the server data transfer, that is, the client tells the server: I will not give you send the data (of course, before the fin package data sent out, if the corresponding ack not received confirmation packet, the client will still retransmit the data), but this time the client can accept data.
  • 第二次挥手:服务端收到 FIN 包后,发送一个 ACK 给客户端,确认序号为收到序号 + 1(与 SYN 相同,一个 FIN 占用一个序号)。
  • 第三次挥手:服务端发送一个 FIN,用来关闭服务端到客户端的数据传送,也就是告诉客户端,我的数据也发送完了,不会再给你发数据了。
  • 第四次挥手:客户端收到 FIN 后,发送一个 ACK 给服务端,确认序号为收到序号 + 1,至此,完成四次挥手。

常见问题

1.第 3 次握手失败会怎么办?

第三次失败,只有客户端处于成功状态(因为第 2 次服务器返回了 ACK),服务器端没有接收到客户端的 ACK。

这要分几种情况讨论:

  • 客户端发出的 ACK 丢失了,发出的 下一个数据包 没有丢失,则服务端接收到下一个数据包(这个数据包里也会带上 ACK 信息),能够进入正常的 ESTABLISHED 状态
  • 如果服务端和客户端都没有数据发送,或者服务端想发送数据(但是发不了,因为没有收到客户端的 ACK),服务器都会有定时器发送第二步 SYN+ACK 数据包,如果客户端再次发送 ACK 成功,建立连接。
  • 如果一直不成功,服务器肯定会有超时设置,超时之后会给客户端发 RTS 报文,进入 CLOSED 状态,防止 SYN 洪泛攻击。

2.为什么 TCP 链接需要三次握手,两次不可以么,为什么?

为了防止已失效的链接请求报文突然又传送到了服务端,因而产生错误。

Connected client sends a request message is not lost, but in the long stay of a network node , resulting in delayed to some time after the release of the link before reaching Server. That is, Server mistakenly think that this is a new link request issued by the Client, so he sends an acknowledgment packet to the client, agreed to establish a link. If a "three-way handshake", so long as the Server send a confirmation packet, the new link is established. As the client makes a request at this time it did not establish a link, so it does not ignore the confirmation Server, nor communicate with the Server; and then waiting for Client Server has been requested, so that Server is wasted certain resources . If a "three-way handshake", in this case, because the Server does not receive confirmation from the client, you will know Client does not require the establishment of a request, it will not establish the link.


3. Why is the time of connection is three-way handshake, but it is closed when the four-way handshake?

TCP is a full-duplex mode , when the connection is closed, when the host B FIN packet is received host A, the host A represents not only the data but also receive transmitted data. At this time, host B may not have all the data sent to the A, so B may close immediately; some data may also be sent to the A, and then send a FIN packet to the other party to agree to close the connection now, therefore, the host and FIN BACK usually sent separately.

Guess you like

Origin blog.csdn.net/qq_33945246/article/details/97290177