Any good example of Non-blocking UDP read?

  • Thread starter Thread starter Tash Robinson
  • Start date Start date
T

Tash Robinson

I have been searching and can not seem to find a good example of a
working UDP client that listens, but does not block while waiting for
data arrival.

I am trying to write a client application that needs to listen on
several TCP ports and one UDP port. It will send a start signal over
UDP and then a repeated status message of a fixed length will be
returned from the server. I need to listen to these messages, but I
can't stop the program execution if no message is available yet like
the UdpClient.Receive method does. I have found several example of
async TCP but can not figure out how this would work with UDP.

Does anyone have a working example of a non-blocking UDP client, or
can someone give me some guidance in this area?

Thanks!
 
Tash,

While this isn't an example, this can be done fairly easily with
Socket.BeginReceiveFrom(). Pass this function a delegate, and that delegate
will be called right away from a separate thread. In this delegate you
should then call EndReceiveFrom(), which will block until data is read --
but remember, this blocking happens on a separate thread, so your main
thread can continue to run.
 
Tash Robinson said:
I have been searching and can not seem to find a good example of a
working UDP client that listens, but does not block while waiting for
data arrival.

I am trying to write a client application that needs to listen on
several TCP ports and one UDP port. It will send a start signal over
UDP and then a repeated status message of a fixed length will be
returned from the server. I need to listen to these messages, but I
can't stop the program execution if no message is available yet like
the UdpClient.Receive method does. I have found several example of
async TCP but can not figure out how this would work with UDP.

Does anyone have a working example of a non-blocking UDP client, or
can someone give me some guidance in this area?

Put the socket in non-blocking mode and use Select() to check for
any number of readable file descriptors. That's the standard way
of doing this. (Currently, Select() can't be used to block indefinitely --
see the concurrent thread with subject "Blocking Select() does not work."

Cheers,

Michi.
 
Back
Top