Discussion:
IOCP: Post-Completion Error Codes for ConnectEx
(too old to reply)
Michael K. O'Neill
2008-02-25 01:42:25 UTC
Permalink
I'm using ConnectEx() as part of a stress-test client for an IOCP server, to
hammer the server with many simultaneous connections. I know that some of
the connection requests will fail, and I'm trying to determine (in the
client's code) when they do. But I can't figure out how to do so in the
completion thread.

The scenario is ordinary: call ConnectEx() with a NULL SendDataBuffer.
WSAGetLastError returns WSA_IO_PENDING. I get the completion notification
in a worker thread that's waiting on GetQueuedCompletionStatus(). Now, how
can I determine that the connection request has failed, or was successful?

With other socket architectures, this information is readily available.
With WSAAsyncSelect-based architectures, when you get an FD_CONNECT, the
connection error codes are in the HIWORD of the message's lParam. With
WSAEventSelect-based architectures, the call to WSAEnumNetworkEvents gives
you a WSANETWORKEVENTS structure, and you can use FD_CONNECT_BIT to index
into the error codes of the iErrorCode member.

If I were using these architectures, I would be looking for the
WSAECONNREFUSED error.

But I can't figure out how to do something similar for IOCP and ConnectEx().
If the connection request failed at the time of the call to ConnectEx(),
then of course I can see the WSAECONNREFUSED error. But I can't see how to
do that in the completion routine, after the call to ConnecEx() has
completed.
David Schwartz
2008-02-25 05:07:47 UTC
Permalink
On Feb 24, 5:42 pm, "Michael K. O'Neill"
Post by Michael K. O'Neill
I'm using ConnectEx() as part of a stress-test client for an IOCP server, to
hammer the server with many simultaneous connections. I know that some of
the connection requests will fail, and I'm trying to determine (in the
client's code) when they do. But I can't figure out how to do so in the
completion thread.
The scenario is ordinary: call ConnectEx() with a NULL SendDataBuffer.
WSAGetLastError returns WSA_IO_PENDING. I get the completion notification
in a worker thread that's waiting on GetQueuedCompletionStatus(). Now, how
can I determine that the connection request has failed, or was successful?
Call WSAGetOverlappedResult.
http://msdn2.microsoft.com/en-us/library/aa921087.aspx

DS
Michael K. O'Neill
2008-02-26 16:14:42 UTC
Permalink
Post by David Schwartz
On Feb 24, 5:42 pm, "Michael K. O'Neill"
Post by Michael K. O'Neill
I'm using ConnectEx() as part of a stress-test client for an IOCP server, to
hammer the server with many simultaneous connections. I know that some of
the connection requests will fail, and I'm trying to determine (in the
client's code) when they do. But I can't figure out how to do so in the
completion thread.
The scenario is ordinary: call ConnectEx() with a NULL SendDataBuffer.
WSAGetLastError returns WSA_IO_PENDING. I get the completion notification
in a worker thread that's waiting on GetQueuedCompletionStatus(). Now, how
can I determine that the connection request has failed, or was successful?
Call WSAGetOverlappedResult.
http://msdn2.microsoft.com/en-us/library/aa921087.aspx
DS
Thank you both, David and Len, for pointing me to the
WSAGetOverlappedResult() function.

I suppose I should have realized this for myself. But in fairness, the
documentation for WSAGetOverlappedResult() only mentions WSASend(To) and
WSARecv(From); it doesn't mention ConnectEx() or other functions to which
WSAGetOverlappedResult() probably also applies (like AcceptEx and
DisconnectEx).

Len Holgate
2008-02-25 10:13:09 UTC
Permalink
Post by Michael K. O'Neill
The scenario is ordinary: call ConnectEx() with a NULL SendDataBuffer.
WSAGetLastError returns WSA_IO_PENDING.  I get the completion notification
in a worker thread that's waiting on  GetQueuedCompletionStatus().  Now, how
can I determine that the connection request has failed, or was successful?
If your call to GetQueuedCompletionStatus returns you can call
GetLastError(). If the failure is IOCP related then the ppOverlapped
that you passed in to GetQueuedCompletionStatus will remain null, if
this isnt null but the function returned false then the error relates
to the overlapped I/O operation that has completed. If that was a
connectEx attempt the the error code that you get from GetLastError()
is what you would have expected to get after the ConnectEx() call
failed (had it been a synchronous call).

Or call WSAGetOverlappedResult...

Len
Loading...