GetQueuedCompletionStatus returns ERROR_NETNAME_DELETED on remote socket closure
-
19-04-2021 - |
Question
I am writing a small server-client-stuff using an I/O-Completion Port.
I get the server and client connected successfully via AcceptEx over my completion port. After the client has connected the client socket is associated with the completion port and an overlapped call to WSARecv on that socket is invoked.
Everything works just fine, until I close the client test program.
GetQueuedCompletionStatus()
returns FALSE
and GetLastError
returns
ERROR_NETNAME_DELETED
, which makes sense to me (after I read the description on the MSDN).
But my problem is, that I thought the call to GetQueuedCompletionStatus
would return me a packet indicating the failure due to closure of the socket, because WSARecv
would return the apropriate return value.
Since this is not the case I don´t know which clients´ socket caused the error and cant act the way i need to (freeing structures , cleanup for this particular connection, etc)...
Any suggestion on how to solve this, Or hints?
Thanks:)
EDIT: http://codepad.org/WeYINasO <- the code responsible... the "error" occures at the first functions beginning of the while-loop (the call to GetCompletionStatus()
which is only a wrapper for GetQueuedCompletionStatus() working fine in other cases) [Did post it there, because it looks shitty & messy in here]
La solution
Here are the scenarios that you need to watch for when calling GetQueuedCompletionStatus
:
GetQueuedCompletionStatus
returnsTRUE
: A successful completion packet has been received, all the out parameters have been populated.GetQueuedCompletionStatus
returnsFALSE
,lpOverlapped == NULL
: No packet was dequeued. The other out parameters contain indeterminate values.GetQueuedCompletionStatus
returnsFALSE
,lpOverlapped != NULL
: The function has dequeued a failed completion packet. The error code is available viaGetLastError
.
To answer your question, when GetQueuedCompletionStatus
returns FALSE
and lpOverlapped != NULL
, there was a failed I/O completion. It's the value of lpOverlapped
that you need to be concerned about.
Autres conseils
I know this is an old question, but I found this page while fruitlessly googling for details about ERROR_NETNAME_DELETED
. It is an error which I get while doing an overlapped Readfile()
.
After some debugging it turned out that the problem was caused by a program which was writing to a socket but forgetting to call closesocket()
before using ExitProcess()
(due to garbage collection issues). Calling CloseHandle()
did not prevent the error, nor did adding WSACleanup()
before ExitProcess()
. However, adding a short sleep before the client exited did prevent the error. Maybe avoiding ExitProcess()
would have prevented the problem also.
So I suspect your problem is caused by the program exiting without closing down the socket properly.
I don't think this would be an issue on Unix where sockets are just ordinary file descriptors.