为什么当"-2"返回IP地址时返回的IP地址不同?被添加在地址长度后面吗? [英] Why the returns IP address is different when "-2" is added behind address length?

查看:91
本文介绍了为什么当"-2"返回IP地址时返回的IP地址不同?被添加在地址长度后面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我发布了此问题, ^ ]
我从其他网站得到了答案.

当我使用

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";
    }
}



我获得了IPv6格式的IP地址,
但是当我使用以下代码时,



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>



我获得了IPv4格式的IP地址,并且我注意到 addr.length-2
改变了结果,但我仍然不明白为什么"-2"会改变结果,有人知道这背后的原因/不可思议吗?



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?

推荐答案

您似乎不了解地址列表实际上是一个列表.
返回值包含多个值,其中一些地址为ipv4,而某些地址为ipv6.如果您依赖长度2之一是ipv4的事实,那么您的编程风格很差.
相反,您应该做的是遍历所有地址,然后检查地址族.
http://msdn.microsoft.com/zh-CN /library/system.net.ipaddress_members(v=vs.80).aspx [ http://msdn.microsoft.com/en -us/library/system.net.ipaddress.addressfamily(v = vs.80).aspx [
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;
            }
        }


这篇关于为什么当"-2"返回IP地址时返回的IP地址不同?被添加在地址长度后面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆