tags:

views:

83

answers:

2

I used this hostInfo.AddressList to get the machine ip address. It returns it in the letter format, such as ff80::c9c9:b2af:aa0f:e2d2%12, what i want is to format it to a ip address format (digits).

Any help?

using c#, .net 3.5

+5  A: 

That is an ip address format, specifically an IPv6 IP address. It sounds like you want an IPv4 address, but IPv6 addresses cannot be down-converted into IPv4 addresses without knowing how your network is configured.

If your host has a v6 address, I suggest that you stick with it. You certainly shouldn't be trying to down-convert addresses at the application level.

JSBangs
so basically, there is no way to get a IPv4 format at all in this case?
gnomixa
Nope, there is no dotted quad form of an IPv6 address, because IPv6 addresses are not 32-bit.
Ben Voigt
+5  A: 

The format you gave is the correct way to represent an IPv6 address. There does not exist an A.B.C.D format to represent IPv6 addresses.

What's happening is you are getting a list of addresses both IPv4 and IPv6. You're looking for the IPv4 ones.

string GetFirstIPv4Address()
{
    IPAddress[] addressList = Dns.GetHostAddresses(hostname);

    foreach (IPAddress ip in addressList)
    {
        if (ip.AddressFamily.ToString() == "InterNetwork")
        {
            //This is an IPv4 address
            return ip.ToString();
        }
    }
    return "127.0.0.1";
}
Brian R. Bondy