从IPHostEntry获取有效IP [英] Getting valid IP from IPHostEntry

查看:64
本文介绍了从IPHostEntry获取有效IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用此地址获取计算机的IP地址

I tried to get the IPAddress of my computer using this

        var ipadd = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ipAddress in ipadd.AddressList)
            Console.WriteLine("IP Address: {0}", ipAddress);

我的计算机中只有一个网卡连接到路由器.它是ipv4,但是这行代码给了我4个IPAddress,其中3个是ipv6,一个是ipv4,这是有效的.我想问为什么会这样?

I have only one network card in my computer which is connected to the router. It is ipv4 but this line of code gives me 4 IPAddress 3 of them are ipv6 and one is ipv4 which is the valid one. I like to ask why is that so ?

谢谢

推荐答案

foreach (var addr in Dns.GetHostEntry(string.Empty).AddressList)
{
    if (addr.AddressFamily == AddressFamily.InterNetwork)
        Console.WriteLine("IPv4 Address: {0}", addr)
}

这篇关于从IPHostEntry获取有效IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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