UdpClient.Receive issue

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

Hello,

I'm stuck on a problem with the UdpClient.Receive method. I'm writing an app
that listens on
a multicast address for UDP packets sent from another machine. It uses the
UdpClient.Receive method to block while listening for a packet. When a
packet is received, it will return and allow me to
process the data.

The problem is, if I release/renew my IP address while in the blocking
Receive() call, it will block forever. Even though I successfully
reestablish network connectivity and there are plenty of UDP packets
available, this call never returns. Ideally Receive() should throw a socket
error when I lose network connectivity.

I can always force this call to throw by calling UdpClient.Close() on
another thread, but this would require another thread just to monitor
network connectivity and make the UdpClient throw when it
should. I'd hate to have to add another thread just for that.

Is there a better way to do this?

Thank you,

-Jim
 
Jim

You will not receive an exception with UDP because it is an inherently connectionless protocol, so according to your listener, it never lost connectivity, it is just sitting there blocking like you told it to.

I personally recommend avoiding the blocking calls. I have found the exception handling to be less than robust. I recommend you code using the asynch calls. Even though it is a little more work, it is much more robust once you get it functioning.

Also, if you use the asych methods you don't have to mess with any sort of polling mechanism to determine if your conneciton is still alive

Hope this helps
-Cla

Clayton Gulick, MCSD
 
Its fine with me that the call keeps blocking, but once I've got nework
connectivity again I want it to start receiving packets again. A temporary
loss in network connectivity seems to render the UdpClient totally
nonfunctional.

I guess I'll just use the Socket class directly then, since the UdpClient
class doesn't expose async methods.

thanks for the help!


Clayton Gulick said:
Jim,

You will not receive an exception with UDP because it is an inherently
connectionless protocol, so according to your listener, it never lost
connectivity, it is just sitting there blocking like you told it to.
I personally recommend avoiding the blocking calls. I have found the
exception handling to be less than robust. I recommend you code using the
asynch calls. Even though it is a little more work, it is much more robust
once you get it functioning.
Also, if you use the asych methods you don't have to mess with any sort
of polling mechanism to determine if your conneciton is still alive.
 
Hi Jim,

I agree with Clay's suggestion. Usually a UDP server needs a way to handle
multiple clients at the same time. Normally a client starts, immediately
communicates with a single server, and is done. Servers, on the other hand,
start and then go to sleep, waiting for a client's datagram arrives. So
asynchronized mode will be a good solution.

Here is a link you may take a look.
http://groups.google.com/groups?q=UdpClient+asynchronous&hl=zh-CN&lr=&ie=UTF
-8&oe=UTF-8&selm=%23dfSAR8aDHA.2336%40TK2MSFTNGP09.phx.gbl&rnum=2

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top