简体   繁体   中英

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
  2. create a TCP socket that connects to the server
  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:

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. 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.

Why does this happen?

Note: the above capture was done on Windows 8.1.

The sender cannot send data after a [FIN]. Such an action will result in the receiver issuing an [RST].

The FIN probably indicates that the server has fully closed the connection in both directions. In this case if it receives any further data on the connection it will issue an 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. Or possibly you're just doing it wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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