How to use WSAEnumNetworkEvents ??
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.
Code:
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
Re: How to use WSAEnumNetworkEvents ??
There is an example of using WSAEnumNetworkEvents here.
After calling WSAEnumNetworkEvents you can use the WSANETWORKEVENTS structure to determine which events that has occured and if they failed or not.
- petter
Re: How to use WSAEnumNetworkEvents ??
Use two events and wait for MultipleObject is one alternative....
Re: How to use WSAEnumNetworkEvents ??
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:
Code:
// 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:
Code:
// 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...