处理 Socket.ReceiveFrom 超时而不发送垃圾邮件控制台
我正在编写一个 ServerLocator,它基本上广播一个端口来查找将使用 IPEndPoint 进行响应的服务器,并且如果在当前 IPHost 上找不到任何内容,我需要搜索能够超时,然后继续进行下一个。
现在我正在做类似的事情(我已经删除了此代码的某些部分,因此它只包含显示我的问题所需的内容。这里还有一些客户端绑定)
string serverIp = string.Empty;
while(string.isNullOrEmpty(serverIp))
{
foreach (IPAddress adress in ipHosts.AddressList)
{
using(Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
{
try
{
client.ReceiveFrom(buffer, ref tempRemoteEP);
//Get server IP
serverIp = tempRemoteEP.ToString().Split(":".ToCharArray(), 2)[0];
break;
}
catch(SocketException e)
{
// We expect connection attempt to time out if we cant find any server on this port and nic. Just continue with the next
if (e.SocketErrorCode == SocketError.TimedOut)
{
continue;
}
}
}
}
}
这按预期工作,除了控制台收到垃圾邮件:
System.dll 中发生了“System.Net.Sockets.SocketException”类型的第一次机会异常
是否有一种好方法可以处理此类异常而不向控制台发送垃圾邮件?或者我可以用其他方式处理这个问题以避免超时异常吗?
谢谢。
I am writing a ServerLocator that basically broadcast a port to find a server which will respond with an IPEndPoint and I need the search to be able to timeout if nothing is found on the current IPHost and then move on with the next one.
Right now I am doing something like this (I have removed some parts of this code so it only includes what is needed to display my problem. There is also some client bindings going on here)
string serverIp = string.Empty;
while(string.isNullOrEmpty(serverIp))
{
foreach (IPAddress adress in ipHosts.AddressList)
{
using(Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
{
try
{
client.ReceiveFrom(buffer, ref tempRemoteEP);
//Get server IP
serverIp = tempRemoteEP.ToString().Split(":".ToCharArray(), 2)[0];
break;
}
catch(SocketException e)
{
// We expect connection attempt to time out if we cant find any server on this port and nic. Just continue with the next
if (e.SocketErrorCode == SocketError.TimedOut)
{
continue;
}
}
}
}
}
This works as expected except that the console gets spammed with:
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
Is there a good way to handle exceptions like this without spamming the console? Or could I handle this in some other way to be able to avoid the need of exception from timeout?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果程序继续运行的话,真的不需要担心这个,程序中会发送很多这样的异常。有关“首先时间例外”。
另请检查此链接以了解如何配置 Visual Studio 来处理例外情况。如果您配置了这些,您可以在异常时中断(而不是继续)并查看到底发生了什么。但是,请注意,隐藏异常似乎在调试中不起作用,请参阅 此处 或here 但正如 @Cipi 指出的那样,它不应该在发布中可见。
There's really no need to worry about this if the program keeps running, there's a lot of these exceptions being sent in a program. See this article for more on "First time exceptions".
Also check this link to see how you can configure Visual Studio on how to handle the exceptions. If you configure these you can break (instead of continue) on the exceptions and see what's really going on. However, do note that hiding the exceptions does not seem to work in debug, see here or here But as @Cipi pointed out, it should not be visible in Release.