Click to See Complete Forum and Search --> : How to use WSAEnumNetworkEvents ??


Shvalb
November 3rd, 2008, 07:29 AM
Hi,

I've this piece of code on my Server, which tries to understand if a certian Client has sent data of has closed his socket.



WSAEVENT hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
ret = WSAEventSelect(m_client, hEvent, FD_READ | FD_CLOSE);

do
{
DWORD dReturn = WaitForSingleObject(hEvent, INFINITE);

int r = recv(m_client, sData, 64, 0);

printf("Status: %d\n", dReturn);
} while(true);



the value of 'dReturn' always equals ZERO and my question is:

How can I distinguish between: FD_READ to FD_CLOSE ??

Thanks

wildfrog
November 3rd, 2008, 07:58 AM
There is an example of using WSAEnumNetworkEvents here (http://msdn.microsoft.com/en-us/library/ms741572(VS.85).aspx).

After calling WSAEnumNetworkEvents you can use the WSANETWORKEVENTS structure to determine which events that has occured and if they failed or not.

- petter

TheCPUWizard
November 3rd, 2008, 07:58 AM
Use two events and wait for MultipleObject is one alternative....

MikeAThon
November 3rd, 2008, 10:14 AM
The sample code given by Microsoft in its WSAEnumNetworkEvents() documentation is not very good.

At the very least, change the timeout in the second call to WSAWaitForMultipleEvents() from 1000 down to zero.

In addition, when checking for network events, remember that more than one network event can be set at one time. So, code like this is wrong:
// bad, don't use

if (NetworkEvents.lNetworkEvents == FD_ACCEPT)
{
// .. processing for FD_ACCEPT
}

// Process FD_READ notification
if (NetworkEvents.lNetworkEvents == FD_READ)
{
// .. processing for FD_READ
}

// Process FD_WRITE notification, etc...
It's wrong for the reason that multiple different FD's can be set for one single network event. Instead, uise code like this:
// better

if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
{
// .. processing for FD_ACCEPT
}

// Process FD_READ notification
if (NetworkEvents.lNetworkEvents & FD_READ)
{
// .. processing for FD_READ
}

// Process FD_WRITE notification, etc...