我有这段代码
public static TcpConnectionInformation[] getConnections()
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpInfoList = properties.GetActiveTcpConnections();
return tcpInfoList;
}
但有时候代码会返回MAC地址(比如:: ffff:0:f7ad:645d)而不是ip,有人知道如何修复它吗?
答案 0 :(得分:4)
这不是MAC地址,而是IPv6地址。您可以将结果过滤为仅显示IPv4地址,如图例所示。
答案 1 :(得分:2)
你试过这个吗?
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;