1

I wrote a C++ application which can act as both server and a client depending upon command line arguments. So, the client will initiate a connection and the server will accept and the data transfer will take place, and I will always have one pending WSARecv call with 2KB buffer. Till here this is fine.

But let's say I call shutdown and closesocket on client, then GetQueuedCompletionStatus fails with error ERROR_CONNECTION_ABORTED (0x40) on the client side and on the server side it fails with error ERROR_NETNAME_DELETED (0x64).

Here is the code for closing the socket

   shutdown(sock, SD_RECEIVE);
   closesocket(sock);

But lets say, I send a request to server from Postman application, I notice when Postman receives a response from server it closes the socket, and GetQueuedCompletionStatus doesn't fail and I get a IO Completion packet, with the lpNumberOfBytesTransferred as zero.

What is the reason for this change of behavior? Am i closing the sockets gracefully?

Also in wireshark I notice when my client closes the socket I see a RST packet, whereas Postman sends a FIN packet. What am I doing wrong?

  • You don't get a zero byte packet. There is no such thing in TCP. The zero is the indication that the peer has closed his end of the connection, and that you should do the same. – user207421 Jun 12 '20 at 10:03
  • @MarquisofLorne I edited my question, to better explain my question. I am getting an IO Completion packet with the lpNumberOfBytesTransferred as zero from GetQueuedCompletionStatus – Sandeep Tadepalli Jun 12 '20 at 11:13
  • iocp here unrelated. and `GetQueuedCompletionStatus` not fail, but return some final status on completed I/O. if you use asynchronous I/O on socket wrong call `shutdown` for disconnect. but need call [`DisconnectEx`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms737757(v=vs.85)) and `closesocket` when the DisconnectEx request completes – RbMm Jun 12 '20 at 19:58

0 Answers0