CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2004
    Posts
    293

    Question 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

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: How to use WSAEnumNetworkEvents ??

    Use two events and wait for MultipleObject is one alternative....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured