
Socket Error: WSAGetOverlappedResult fails sometimes
I have two
MFC programs communicating over multiple sockets. At
present,during development, both programs are running on the same NT
machine. I am using Winsock2, with the overlapped I/0 model.
The programs start out by exchanging a fixed set of messages.
Sometimes the communication fails at a particular point, and sometimes
it works. I haven't been able to figure out a pattern.
The error occurs in the client, which is waiting for an
acknowledgement of a command that it sent to the server. The client is
running WSAWaitForMultipleEvents in a thread. When the event is
signaled, the thread posts a message to the main thread. The main
thread runs WSAGetOverlappedResult, which, when the error occurs,
returns FALSE. The error returned by WSAGetLastError() is 995. 995 is
a system error code called ERROR_OPERATION_ABORTED, "The I/O operation
has been aborted because of either a thread exit or an application
request. " I can't figure out how to apply this message to my
situation.
The server is not reporting any errors.
Thanks very much !
Elaine
Here are some fragments of the code:
// The following code runs in a thread on the client
while (TRUE)
{
if ((Index = WSAWaitForMultipleEvents(MAXCONNECTIONS,
ptp->EventArray,FALSE,WSA_INFINITE,FALSE))
== WSA_WAIT_FAILED)
{
// handle error
}
Index -= WSA_WAIT_EVENT_0;
WSAResetEvent(ptp->EventArray[Index])
// post a message to the main thread, so that the main thread
handles the
// socket event
while (::PostMessage(ptp->hWnd,SOCKET_THREADMESSAGE,
(WPARAM) Index,(LPARAM)0) != TRUE)
{
}
}
// Code in the main thread. This is the call that fails.
DWORD BytesTransferred=0;
DWORD Flags;
BOOL retval=TRUE;
WSASetLastError(0);
if ((retval=WSAGetOverlappedResult(m_Socket,&m_Overlapped,&BytesTransferred,
FALSE,&Flags))== FALSE || BytesTransferred == 0)
{
sprintf(m_errMsg," error %d",
WSAGetLastError());
}