如何检查TCP连接是否关闭,是否为IPV6?



我使用这段代码来检查TCP连接是否关闭。然而,在使用此代码时,我注意到如果连接使用IPV4,它不适用于IPV6地址:

if (!socket.Connected) return false;
var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
var tcpConnections = ipProperties.GetActiveTcpConnections()
.Where(x => x.LocalEndPoint.Equals(socket.LocalEndPoint) && x.RemoteEndPoint.Equals(socket.RemoteEndPoint));
var isConnected = false;
if (tcpConnections != null && tcpConnections.Any())
{
TcpState stateOfConnection = tcpConnections.First().State;
if (stateOfConnection == TcpState.Established)
{
isConnected = true;
}
}
return isConnected;

在调试链接答案中的代码时,我注意到返回一个包含以下端点的列表:

{127.0.0.1:50503}

然而,我正在测试的套接字似乎是IPV6:

{[::飞行符:127.0.0.1]:50503}

{127.0.0.1:50503} == {[::ffff:127.0.0.1]:50503}返回false,因此检查失败。

如何测试IPV4地址和IPV6地址是否指向同一主机?

最后我创建了一个"促销"如果另一方是IPV6,则指向IPV6的端点:

public static bool IsConnectionEstablished(this Socket socket)
{
if (socket is null)
{
throw new ArgumentNullException(nameof(socket));
}
if (!socket.Connected) return false;
var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
var tcpConnections = ipProperties.GetActiveTcpConnections()
.Where(x => x.LocalEndPoint is IPEndPoint && x.RemoteEndPoint is IPEndPoint && x.LocalEndPoint != null && x.RemoteEndPoint != null)
.Where(x => AreEndpointsEqual(x.LocalEndPoint, (IPEndPoint)socket.LocalEndPoint!) && AreEndpointsEqual(x.RemoteEndPoint, (IPEndPoint)socket.RemoteEndPoint!));
var isConnected = false;
if (tcpConnections != null && tcpConnections.Any())
{
TcpState stateOfConnection = tcpConnections.First().State;
if (stateOfConnection == TcpState.Established)
{
isConnected = true;
}
}
return isConnected;
}
public static bool AreEndpointsEqual(IPEndPoint left, IPEndPoint right)
{
if (left.AddressFamily == AddressFamily.InterNetwork &&
right.AddressFamily == AddressFamily.InterNetworkV6)
{
left = new IPEndPoint(left.Address.MapToIPv6(), left.Port);
}
if (left.AddressFamily == AddressFamily.InterNetworkV6 &&
right.AddressFamily == AddressFamily.InterNetwork)
{
right = new IPEndPoint(right.Address.MapToIPv6(), right.Port);
}
return left.Equals(right);
}

在创建TCP客户端套接字时,您可以向构造函数传递参数以显式使用ipv4地址

new TcpClient(AddressFamily.InterNetwork);

最新更新