如何在阻塞插座中赶超时

发布于 2025-01-29 13:40:11 字数 378 浏览 1 评论 0原文

我正在尝试使阻止UDP套接字(服务器)

如​​果数据未接收,则socket将永远悬挂。

我想将其纳入5 sec超时。

有任何解决方案吗?

byte[] data = new byte[1024];

socket.Bind(endpoint);         
socket.Blocking = true;

EndPoint tmpRemote = (EndPoint)sender;
while (true)
{
   try
   {
      recv = socket.ReceiveFrom(data, ref tmpRemote);
   }
}

I am trying to make Blocking UDP socket (server)

If data doesn't receive, the socket will hang forever.

I want to make it to 5 sec timeout.

Is there any solution?

byte[] data = new byte[1024];

socket.Bind(endpoint);         
socket.Blocking = true;

EndPoint tmpRemote = (EndPoint)sender;
while (true)
{
   try
   {
      recv = socket.ReceiveFrom(data, ref tmpRemote);
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2025-02-05 13:40:11

插座类包含接收到的属性。您可以为此属性设置任何值,并在超时发生时捕获异常。
请检查此url: https://www.demo2s.com/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/csharp/ -socket-receivetimeout.html

Socket class contain ReceiveTimeOut property . You can set any value to this property and catch exception when timeout occurs .
Please check this url : https://www.demo2s.com/csharp/csharp-socket-receivetimeout.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文