开发者

GetHostEntry.AddressList[0] returns ::1 on current Windows version even with IPv6 off. Can I change this?

开发者 https://www.devze.com 2022-12-18 12:40 出处:网络
I\'m using a third party library suite that was stupidly hard-coded to call GetHostEntry.AddressList[0] for a local IP address. It is also not written to support IPv6. I disabled IPv6 on all my networ

I'm using a third party library suite that was stupidly hard-coded to call GetHostEntry.AddressList[0] for a local IP address. It is also not written to support IPv6. I disabled IPv6 on all my network interfaces, but AddressList[0] in my test program (and in the 3rd party libraries) still returns {::1} rather than my first IPv4 address. Is there any Windows setting I can change to fix this so that it behaves like Windows XP (which 开发者_JAVA技巧returns the first IPv4 address)?

Here is the test program I'm using to verify the behavior:

   class Program
   {
    static void Main(string[] args)
    {
     List<string> addresses = ( from address in Dns.GetHostEntry(Dns.GetHostName()).AddressList select address.ToString() ).ToList();
   foreach (string a in addresses)
     {
      Console.WriteLine(a);
     }
     Console.Read();
    }
 }

On a Windows XP machine, the output of the program is 192.168.56.1

On my Windows 7 machine, the output of the program is ::1 192.168.56.2

Any suggestions? Changing the third party library code is not an option available to me.


Each member of IPHostEntry.AddressList is a IPAddress which has a property AddressFamily which you can use to filter for a specific family.

E.g. IPv4 addresses only:

from address in Dns.GetHostEntry(Dns.GetHostName()).AddressList
where address.AddressFamily == AddressFamily.InterNetwork
select address.ToString()

(Change to AddressFamily.InterNetworkV6 to limit to IPv6 addresses.)

EDIT: Clearly this is a code change, so either (1) filter at the interface to the third party library, (2) get a "better" library, or (3) it is a feature and make your application work with IPv6 (which it is likely to need in the next few years anyway).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号