Re: Asynchronous UDP Sockets ~ No Read events!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 10/26/04


Date: Tue, 26 Oct 2004 09:59:01 -0700

First, I hope all your if-s fall through - do not continue. Otherwise
you'll miss multiple notifications delivered with a single event.

Second, by clearing your event explicitly you create a race condition
and can miss notifications. WSAEnumNetworkEvents already
does this for you, so take out your explicit reset.

-- 
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Man Lee" <noreply@this.address.com> wrote in message 
news:OZU4Mb3uEHA.4028@TK2MSFTNGP15.phx.gbl...
> Hi all,
>
> I have a UDP client set up asynchronously.
> I never get any FD_READ events.
> Just curious if I'm missing something...
>
> Here is what I have:
>
> Using Winsock 2.2 on XP via VS2003
> In a thread I have the following loop:
>
> SOCKET s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
> bind(s, (struct sockaddr*)&my_addr, sizeof(my_addr));
> WSAEventSelect(s, hEvent, FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
> while (1)
> {
>    //Error checking codes are removed
>
>    WSAWaitForMultipleEvents(1, pEvents, FALSE, 30000, FALSE);
>
>    WSANETWORKEVENTS NetworkEvents = {0};
>    WSAEnumNetworkEvents(s, hEvent, &NetworkEvents);
>
>    if (NetworkEvents.lNetworkEvents & FD_CONNECT)
>    {
>        // Notify a Connect Event
>        // This fires!!!
>        // When I call connect via UDP
>    }
>
>    if (NetworkEvents.lNetworkEvents & FD_READ)
>    {
>        // Notify a Read Event
>        // Nothing ever happens... :(
>    }
>
>    if (NetworkEvents.lNetworkEvents & FD_WRITE)
>    {
>        // Notify a Write Event
>
>        // This fires!!!
>        // When immediately to signal I am able to send.
>    }
>
>    if (NetworkEvents.lNetworkEvents & FD_CLOSE)
>    {
>        // Notify a Close Event
>        // I don't think this will actually ever happen for a UDP socket
>        // But it is here for the heck of it...
>    }
>
>    WSAResetEvent(hEvent);
>
>    // There is some logic here to exit the loop when necessary that is
> removed...
> }
>
> As the comments above say, the FD_CONNECT and FD_WRITE events fire.
> So I am poisitive that there is nothing wrong in how I use 
> WSAEventSelect()
> and its sibiling methods.
>
> Here is what takes the cake:
> When the above code is used against a TCP connection, it works fine,
> FD_READ fires accordingly.
>
> When the above code is used against a UDP socket, it doesn't work.
> BUT, when I direct my client to send packets to itself, the FD_READ does
> fires!
> It only doesn't fire when packets arrive from another computer.
> I snoop my incoming data and can confirm packets are being recv'ed by my 
> NIC
> from the other computer.
> So, the packets are there, but the event is not firing...
>
> There are also no errors being returned in the loop, so that is not the
> issue.
> I have a feeling it has to do with some strange network setup that is on 
> my
> computer...
>
> Does any one have similiar problems?
> If so, how can it be resolved?
>
> Thanks for any help!
> -man.
>
> 


Relevant Pages

  • Asynchronous UDP Sockets ~ No Read events!
    ... I have a UDP client set up asynchronously. ... the FD_CONNECT and FD_WRITE events fire. ... It only doesn't fire when packets arrive from another computer. ... There are also no errors being returned in the loop, ...
    (microsoft.public.win32.programmer.networks)
  • Re: CAsyncsocket derived class being run as a thread
    ... Can I access the member functions like this: ... messages from the specified port. ... will the UDP packets still be received? ... packet listener to do it in a loop. ...
    (microsoft.public.vc.mfc)
  • Re: Enter date/time into table
    ... Because AfterUpdate is *too late*. ... setting a bound column to Now will cause the BeforeUpdate event to fire ... loop de loop! ...
    (microsoft.public.access.forms)
  • Re: CAsyncsocket derived class being run as a thread
    ... I did ask a rudimentary question about threading but its disappeared ... messages from the specified port. ... will the UDP packets still be received? ... packet listener to do it in a loop. ...
    (microsoft.public.vc.mfc)
  • Re: Simple window application with a loop and a button to eventually stop the loop
    ... My experience with infinate loops are, that they take up all the cpu time ... and windows will not even respond to an ctrl-c in a console appz ... the button's click event handler should fire. ... Note that the issue here is not so much the infinite loop as it is that without a separate thread, the loop doesn't simply keep the CPU occupied -- it keeps occupied the one thread that could process user input, by never returning from the event handler that was being processed in order to start the loop. ...
    (microsoft.public.dotnet.languages.csharp)