如何在阻塞插座中赶超时
我正在尝试使阻止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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
插座类包含接收到的属性。您可以为此属性设置任何值,并在超时发生时捕获异常。
请检查此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