gpt4 book ai didi

c# - 如何找出导致 SocketException、UdpClient 的端点

转载 作者:行者123 更新时间:2023-11-30 13:05:02 34 4
gpt4 key购买 nike

我在服务器端使用 UdpClient,它正在向客户端(多个客户端)发送数据。客户端突然停止监听 udp 端口​​,并且服务器在调用 endRecieve 或 beginRecieve 时遇到 SocketException。

据我了解,这是因为“ICMP 目标无法到达”,它只是告诉服务器端口已关闭。没关系,但是 SocketExceptions 都没有告诉我它来自哪个端点。

我如何知道哪个端点已关闭,以便服务器停止向它发送并导致更多 SocketExceptions?

或者有没有办法让 Udpclient 停止抛出这些 SocketExceptions,这样我就可以让客户端超时,如果他们在某几秒后没有响应。

最佳答案

我自己也在处理同样的问题,所以我很想看看是否有人提出了更好的解决方案,但现在我有几个想法:

我的套接字周围有一个 comm 包装类(我们称它为 AsyncComm),它在构造时从其所有者类传递了一个异常处理程序委托(delegate)。异常处理程序委托(delegate)采用异常参数和对引发异常的 AsyncComm 实例的引用。然后我把

try
{
// Do stuff here
{
catch (Exception e)
{
CallExceptionHandlerDelegate(e, this);
}

AsyncComm 中我的每个异步处理程序方法中,这样它们就可以在链上抛出它们的异常。在我的例子中,异常处理程序使用对 AsyncComm 实例的引用来调用 AsyncComm 实例中的方法,以告诉它重新初始化其套接字。您可以将该行为更改为您需要做的任何事情,以停止不断收到 SocketExceptions

关于确定异常来自的终点,我现在唯一的想法是从 SocketException.Message 字符串的末尾解析终点,但这似乎很麻烦。

更新:这是一个拼凑但有效的方法。解析下面的代码,其中一些来自 this question .

private IPEndPoint parseEndPointFromString(string input)
{
// Matches 1-255.1-255.1-255.1-255:0-65535. I think.
const string IPPortRegex = @"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)";

Match match = Regex.Match(input, IPPortRegex);

if (match.Success)
{
string IPPortString = match.Value;

string[] ep = IPPortString.Split(':');
if (ep.Length != 2) throw new FormatException("Invalid endpoint format");
IPAddress ip;
if (!IPAddress.TryParse(ep[0], out ip))
{
throw new FormatException("Invalid IP address");
}
int port;
if (!int.TryParse(ep[1], out port))
{
throw new FormatException("Invalid port");
}
return new IPEndPoint(ip, port);
}
else
{
throw new FormatException("Invalid input string, regex could not find an IP:Port string.");
}
}

关于c# - 如何找出导致 SocketException、UdpClient 的端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6450158/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com