Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set the timeout for UdpClient in C#?

I am wondering whether I can set a timeout value for UdpClient receive method.

I want to use block mode, but because sometimes udp will lost packet, my program udpClient.receive will hang there forever.

any good ideas how I can manage that?

like image 855
Jack Avatar asked Feb 17 '10 14:02

Jack


1 Answers

There is a SendTimeout and a ReceiveTimeout property that you can use in the Socket of the UdpClient.

Here is an example of a 5 second timeout:

var udpClient = new UdpClient();  udpClient.Client.SendTimeout = 5000; udpClient.Client.ReceiveTimeout = 5000;  ... 
like image 100
sep15ms Avatar answered Oct 01 '22 08:10

sep15ms