Click here to Skip to main content
11,644,286 members (71,006 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# socket
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
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 15-Aug-12 8:52am
Comments
Wes Aday at 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 at 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";
}
}
Wes Aday at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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;
            }
        }
  Permalink  
v2
Comments
Manfred R. Bihy at 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)

  Print Answers RSS
0 OriginalGriff 570
1 jyo.net 484
2 Afzaal Ahmad Zeeshan 413
3 DamithSL 308
4 Sergey Alexandrovich Kryukov 276
0 OriginalGriff 795
1 DamithSL 611
2 Mika Wendelius 494
3 jyo.net 484
4 Afzaal Ahmad Zeeshan 404


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100