UDP Socket Win32 Help 
Author Message
 UDP Socket Win32 Help

How do I use UDP w/ asynchronous sockets?? I've created
the socket w/ the SOCK_DGRAM option, and created  
WSACreateEvent & WSAEventSelect to catch events for the
socket. But I seem to get an anomolous FD_WRITE on the
socket when it first comes up, and then my thread
suspends, and for some reason, my entire application
suspends to.

Does a sendto generate a FD_WRITE?? What do I do if a
sendto call doesn't send all the data in my buffer?? Do I
use the FD_WRITE to test for that and send the 'rest' of
the data afterwards?

I'm not using MFC, so I need a pure win32 solution.
Thanks
-C

My loop looks like this:

while (TRUE){
nRetCode = ::WSAWaitForMultipleEvents(1, &hSocketEvent,
                               FALSE, INFINITE, FALSE);    

if (nRetCode != WSA_WAIT_FAILED)
{
::WSAEnumNetworkEvents(SocketID, hSocketEvent,
                      &hNetworkEvents);

if ( (hNetworkEvents.lNetworkEvents & FD_READ) &&
    (hNetworkEvents.iErrorCode[FD_READ_BIT] == 0) )
   {                    
   // Read from socket using recvfrom
   }
else if ( (hNetworkEvents.lNetworkEvents & FD_WRITE) &&
    (hNetworkEvents.iErrorCode[FD_WRITE_BIT] == 0) )
  {}
else if ( (hNetworkEvents.lNetworkEvents & FD_CLOSE) &&
    (hNetworkEvents.iErrorCode[FD_CLOSE_BIT] == 0) )
  {
    // Close socket
    break;
  }
else  
  {
  //WSAResetEvent(hSocketEvent);
  printf ("Received Socket event other than, Read, Write,
Close \n);
  }    
else  // nRetCode == WSA_WAIT_FAILED)
 {
   WSAResetEvent (hSocketEvent);
   break;
 }                                      

Quote:
} // end of while  



Mon, 05 Jul 2004 01:15:01 GMT  
 UDP Socket Win32 Help
It is normal to get FD_WRITE notification right after WSAEventSelect -
the socket is telling you that it is ready to transmit data. It is
normal to receive FD_WRITE soon after sendto - the socket tells you that
it has finished sending your current datagram and is ready to transmit
more data. This is assuming sendto returned WSAEWOULDBLOCK error,
otherwise the socket is ready to send more data immediately and will not
generate FD_WRITE.

sendto with UDP sockets does not send partial data. The datagram is sent
all at once or not at all. You are probably confusing it with the
behavior of TCP sockets.

You will never receive FD_CLOSE notification on a UDP socket. FD_CLOSE
notifies a TCP socket that the connection is terminated by remote peer.
UDP socket does not maintain a connection nor does it have a permanent
remote peer.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> How do I use UDP w/ asynchronous sockets?? I've created
> the socket w/ the SOCK_DGRAM option, and created
> WSACreateEvent & WSAEventSelect to catch events for the
> socket. But I seem to get an anomolous FD_WRITE on the
> socket when it first comes up, and then my thread
> suspends, and for some reason, my entire application
> suspends to.

> Does a sendto generate a FD_WRITE?? What do I do if a
> sendto call doesn't send all the data in my buffer?? Do I
> use the FD_WRITE to test for that and send the 'rest' of
> the data afterwards?

> I'm not using MFC, so I need a pure win32 solution.
> Thanks
> -C

> My loop looks like this:

> while (TRUE){
> nRetCode = ::WSAWaitForMultipleEvents(1, &hSocketEvent,
>                                FALSE, INFINITE, FALSE);

> if (nRetCode != WSA_WAIT_FAILED)
> {
> ::WSAEnumNetworkEvents(SocketID, hSocketEvent,
>                       &hNetworkEvents);

> if ( (hNetworkEvents.lNetworkEvents & FD_READ) &&
>     (hNetworkEvents.iErrorCode[FD_READ_BIT] == 0) )
>    {
>    // Read from socket using recvfrom
>    }
> else if ( (hNetworkEvents.lNetworkEvents & FD_WRITE) &&
>     (hNetworkEvents.iErrorCode[FD_WRITE_BIT] == 0) )
>   {}
> else if ( (hNetworkEvents.lNetworkEvents & FD_CLOSE) &&
>     (hNetworkEvents.iErrorCode[FD_CLOSE_BIT] == 0) )
>   {
>     // Close socket
>     break;
>   }
> else
>   {
>   //WSAResetEvent(hSocketEvent);
>   printf ("Received Socket event other than, Read, Write,
> Close \n);
>   }
> else  // nRetCode == WSA_WAIT_FAILED)
>  {
>    WSAResetEvent (hSocketEvent);
>    break;
>  }
> } // end of while



Mon, 05 Jul 2004 03:49:24 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. UDP Win32 Socket: "recvfrom" completely blocks

2. UDP Windows Socket connection HELP!!!

3. Help: UDP Socket question

4. Close a UDP socket

5. Q: Writing UDP Socket in C...

6. udp socket

7. UDP Datagram sockets

8. UDP Socket question

9. TCP/UDP sockets in Visual C++.

10. Broadcast over UDP socket

11. UDP socket and InstallShield???

12. Multihreading UDP Socket Server

 

 
Powered by phpBB® Forum Software