Can I set the timeout for UdpClient in C#?

前端 未结 5 878
不知归路
不知归路 2020-12-09 08:05

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 udpCl

5条回答
  •  半阙折子戏
    2020-12-09 08:12

    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;
    
    ...
    

提交回复
热议问题