Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Few days ago , i posted this question,Instead of get the host IP address in IPv4 format, i got IPv6 format[^]
and i got the answer from other website..

When i use
C#
try
    {
        //iphostname = Dns.GetHostName();  // Resolving Host name
        IPHostEntry ipentry = Dns.GetHostEntry(hostLabel.Text);
        IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
        for (int i = 0; i < addr.Length; i++)
        {
            try
            {
                ipLabel.Text=  Convert.ToString(addr[i]) + "\r\n";
            }
            catch
            {
                ipLabel.Text += "IP Address            | " + "\r\n";
            }
        }
    }
    catch
    {
                  | " + "\r\n";
    }
}


i got the IP address in IPv6 format,
but when i use the following code,
<pre lang="xml">
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
ipLabel.Text = addr[addr.Length - 2].ToString();</pre>


i got the IP address in IPv4 format, and i noticed that addr.length-2
changed the outcome, but i still dont understand why "-2" change the outcome, does anyone know the reason/magic behind this?
Posted
Comments
[no name] 15-Aug-12 14:57pm    
What magic? What is magical about taking the addr array and getting the element that is the # elements - 2 out of it?
asdf9009 15-Aug-12 14:59pm    
to be precise, i dont understand how it works...why IPv4 is shows when "addr.Length -2" and it doesn't show when code in for (int i = 0; i < addr.Length; i++)
{
try
{
ipLabel.Text= Convert.ToString(addr[i]) + "\r\n";
}
catch
{
ipLabel.Text += "IP Address | " + "\r\n";
}
}
[no name] 15-Aug-12 15:06pm    
Not knowing what it is that you are seeing and not knowing what "doesn't show" means, from your code I would guess that, ipLabel.Text= Convert.ToString(addr[i]) + "\r\n"; would be your problem. This would only show 1 element out of the array and most likely followed by 2 boxes.

1 solution

What you don't seem to understand is that addresslist is in fact a list.
The return value contains multiple values where some addresses are ipv4 and some are ipv6. If you are relying on the fact that the one of length-2 is ipv4 then you are doing poor style of programming.
What you should do instead is iterate through all addresses and then inspect the addressfamily.
http://msdn.microsoft.com/en-us/library/system.net.ipaddress_members(v=vs.80).aspx[^]
als see
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.addressfamily(v=vs.80).aspx[^]
In your code you are iterating through all addresses and the last will be shown on the label. Since you want only ipv4 addresses the first will do so something like this is better

C#
for(int i=0; i< hostInfo.AddressList.Length; i++)
{
            if (addr[i].AddressFamily.ToString() ==  ProtocolFamily.InterNetworkV6.ToString()){ 
              try
              {
                ipLabel.Text=  Convert.ToString(addr[i]) + "\r\n";
              }
              catch
              {
                ipLabel.Text += "IP Address            | " + "\r\n";
              }
              break;
            }
        }
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 15-Aug-12 15:40pm    
Good answer! 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900