Mataharrie
Mataharrie

Reputation: 43

Getting the IP address instead of the MAC address

I have this code

public static TcpConnectionInformation[] getConnections()
{
    IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
    TcpConnectionInformation[] tcpInfoList = properties.GetActiveTcpConnections();
    return tcpInfoList;
}

But sometimes that code return MAC addresses(like ::ffff:0:f7ad:645d) instead of ip, anybody know how to fix it?

Upvotes: 1

Views: 308

Answers (2)

That's not a MAC address, it's an IPv6 address. You can filter your result to only show IPv4 addresses, as Legend shows.

Upvotes: 4

Neigyl R. Noval
Neigyl R. Noval

Reputation: 6038

Have you tried this?

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;

Upvotes: 2

Related Questions