Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WSARecv question

28 views
Skip to first unread message

Patrick J. McNerthney

unread,
Oct 22, 2002, 3:21:28 AM10/22/02
to
I have a server application which is not correctly detecting all client
disconnects and I am wondering if it might be my use of WSARecv.

Is it possible for WSARecv to have return value of 0 (for a completed
receive) and to return 0 bytes read to indicate a disconnected socket?

In other words, does one have to code for the following situation:

if ( WSARecv(socket,
&wsaBuf,
1,
&dwRead,
&dwFlags,
&overlapped,
NULL
) == 0 ) {
if (dwRead == 0) {
// The client has disconnected!!!
}
}

Pat McNerthney
Icicle Software, Inc.


SenderX

unread,
Oct 22, 2002, 4:44:46 AM10/22/02
to
Since you are using overlapped io the call to WSARecv should look something
like this:

this is a snippet from my ac::WinTools::C_Socket class:

BOOL C_Socket::WsaRecv( LPWSABUF pWsaBufs,
LPWSAOVERLAPPED pWsaOl,
INT iWsaBufCount )
{
// Get ready to recv data
DWORD dwBytesRecv,
dwFlags = 0;

// Try and recv the data
if ( WSARecv( m_hSocket,
pWsaBufs,
iWsaBufCount,
&dwBytesRecv,
&dwFlags,
pWsaOl,
NULL ) == SOCKET_ERROR )
{
// Get and handle the wsa error
DWORD dwWsaErr = WSAGetLastError();

if ( dwWsaErr != WSA_IO_PENDING )
{
return FALSE;
}
}

return TRUE;
}

If this function returns FALSE you will NOT get any sort completion.

If this function returns TRUE you ARE going to get a completion.

If you get a zero-byte completion from a posted WSARecv it means the client
has disconnected and a graceful shutdown should happen.

> Is it possible for WSARecv to have return value of 0 (for a completed
> receive) and to return 0 bytes read to indicate a disconnected socket?

If you didn't provide WSARecv with a overlapped struct then the operation
could complete with zero-bytes right then and there, or you get a
WSAEWOULDBLOCK which means WSARecv needs to be tried again.


Chris Pearson

unread,
Oct 23, 2002, 9:36:16 PM10/23/02
to
Yes: "For connection-oriented sockets, WSARecv can indicate the graceful
termination of the virtual circuit in one of two ways that depend on whether
the socket is byte stream or message oriented. For byte streams, zero bytes
having been read (>>> as indicated by a zero return value to indicate
success, and lpNumberOfBytesRecvd value of zero <<<) indicates graceful
closure and that no more bytes will ever be read."

-- CCP

"Patrick J. McNerthney" <p...@mcnerthney.com> wrote in message
news:Y%6t9.197485$U7.52...@twister.socal.rr.com...

0 new messages