简体   繁体   English

为什么此TCP连接的一个端点发送带有RST标志的数据包?

[英]Why is one endpoint of this TCP connection sending a packet with the RST flag?

I'm writing an application that attempts to do the following: 我正在编写一个尝试执行以下操作的应用程序:

  1. create a TCP server listening on an available port 创建一个监听可用端口的TCP服务器
  2. create a TCP socket that connects to the server 创建一个连接到服务器的TCP套接字
  3. have the server socket write data to the client 让服务器套接字将数据写入客户端
  4. have the server socket close its end of the connection 使服务器套接字关闭其连接的末端
  5. have the client write a message to the server 让客户端向服务器写一条消息

Here's where the problem lies. 这就是问题所在。 When I attempt to run the application, the TCP exchange goes like this: 当我尝试运行该应用程序时,TCP交换如下所示:

The first three packets establish the three-way handshake , and the fourth and fifth packets are the transmission of the data written by the server and its acknowledgement. 前三个数据包建立三向握手 ,第四个和第五个数据包是服务器写入的数据的传输及其确认。

As expected, the server socket sends a packet with the FIN flag set to indicate that it is closing its end of the connection. 如预期的那样,服务器套接字发送一个设置了FIN标志的数据包,以指示它正在关闭其连接的末尾。 The client acknowledges this and then attempts to write its data to the socket. 客户端确认这一点,然后尝试将其数据写入套接字。 The server immediately sends an RST packet, terminating the connection prematurely. 服务器立即发送RST数据包,过早终止连接。

Why does this happen? 为什么会这样?

Note: the above capture was done on Windows 8.1. 注意:以上捕获是在Windows 8.1上完成的。

The sender cannot send data after a [FIN]. 发送者无法在[FIN]之后发送数据。 Such an action will result in the receiver issuing an [RST]. 这样的动作将导致接收机发出[RST]。

The FIN probably indicates that the server has fully closed the connection in both directions. FIN可能表明服务器已双向完全关闭连接。 In this case if it receives any further data on the connection it will issue an RST. 在这种情况下,如果它在连接上收到任何其他数据,它将发出RST。 This suggests an application protocol error on your part. 这表明您有应用程序协议错误。 If the server sends a reply and then closes the socket, the client can't send anything else via that connection. 如果服务器发送答复然后关闭套接字,则客户端无法通过该连接发送任何其他内容。

Possibly you need your server to call shutdown() with SHUT_WR and then read something else from the client before closing the socket. 可能需要服务器使用SHUT_WR调用shutdown() ,然后在关闭套接字之前从客户端读取其他内容。 Or possibly you're just doing it wrong. 也许您只是做错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM