• 获取本机的IPv4或者v6地址


    前言:本文介绍为什么使用Dns.GetHostAddresses()在不同计算机上执行结果不同的原因;并且给出了筛选IPv4或者ipv6地址的代码。

    在XPsp3系统和Win7系统分别执行Dns.GetHostAddresses()方法,得到了不同的结果:

    win7:

    xp:

    xp中在cmd输入如下命令:

    原因可知:xpsp3系统没有安装ipv6协议,下面安装进行测试:

    安装ipv6协议:

    再输入ipv6 if >D:ipv6if.txt

    输出内容如下:

    Interface 6: Ethernet: 本地连接
    
      Guid {14F445CB-3489-474B-B644-5F749E251F46}
    
      uses Neighbor Discovery
    
      uses Router Discovery
    
      link-layer address: b8-ac-6f-c6-09-79
    
        preferred link-local fe80::baac:6fff:fec6:979, life infinite
    
        multicast interface-local ff01::1, 1 refs, not reportable
    
        multicast link-local ff02::1, 1 refs, not reportable
    
        multicast link-local ff02::1:ffc6:979, 1 refs, last reporter
    
      link MTU 1500 (true link MTU 1500)
    
      current hop limit 128
    
      reachable time 15500ms (base 30000ms)
    
      retransmission interval 1000ms
    
      DAD transmits 1
    
      default site prefix length 48
    
    Interface 5: Ethernet: 无线网络连接
    
      Guid {198A6F64-4D3D-47EC-A720-9579E2A3EBDE}
    
      zones: link 5 site 2
    
      cable unplugged
    
      uses Neighbor Discovery
    
      uses Router Discovery
    
      link-layer address: ec-55-f9-44-dc-c0
    
        tentative link-local fe80::ee55:f9ff:fe44:dcc0, life infinite
    
        multicast interface-local ff01::1, 1 refs, not reportable
    
        multicast link-local ff02::1, 1 refs, not reportable
    
        multicast link-local ff02::1:ff44:dcc0, 1 refs, last reporter
    
      link MTU 1500 (true link MTU 1500)
    
      current hop limit 128
    
      reachable time 44000ms (base 30000ms)
    
      retransmission interval 1000ms
    
      DAD transmits 1
    
      default site prefix length 48
    
    Interface 4: Teredo Tunneling Pseudo-Interface
    
      Guid {7F6865DC-7663-4482-BD9E-D689A49C1C3A}
    
      zones: link 4 site 3
    
      cable unplugged
    
      uses Neighbor Discovery
    
      uses Router Discovery
    
      routing preference 2
    
      link-layer address: 0.0.0.0:0
    
        preferred link-local fe80::ffff:ffff:fffd, life infinite
    
        multicast interface-local ff01::1, 1 refs, not reportable
    
        multicast link-local ff02::1, 1 refs, not reportable
    
      link MTU 1280 (true link MTU 1280)
    
      current hop limit 128
    
      reachable time 27000ms (base 30000ms)
    
      retransmission interval 1000ms
    
      DAD transmits 0
    
      default site prefix length 48
    
    Interface 3: 6to4 Tunneling Pseudo-Interface
    
      Guid {A995346E-9F3E-2EDB-47D1-9CC7BA01CD73}
    
      does not use Neighbor Discovery
    
      does not use Router Discovery
    
      routing preference 1
    
      link MTU 1280 (true link MTU 65515)
    
      current hop limit 128
    
      reachable time 29000ms (base 30000ms)
    
      retransmission interval 1000ms
    
      DAD transmits 0
    
      default site prefix length 48
    
    Interface 2: Automatic Tunneling Pseudo-Interface
    
      Guid {48FCE3FC-EC30-E50E-F1A7-71172AEEE3AE}
    
      does not use Neighbor Discovery
    
      does not use Router Discovery
    
      routing preference 1
    
      EUI-64 embedded IPv4 address: 0.0.0.0
    
      router link-layer address: 0.0.0.0
    
        preferred link-local fe80::5efe:192.168.0.60, life infinite
    
      link MTU 1280 (true link MTU 65515)
    
      current hop limit 128
    
      reachable time 41500ms (base 30000ms)
    
      retransmission interval 1000ms
    
      DAD transmits 0
    
      default site prefix length 48
    
    Interface 1: Loopback Pseudo-Interface
    
      Guid {6BD113CC-5EC2-7638-B953-0B889DA72014}
    
      zones: link 1 site 4
    
      does not use Neighbor Discovery
    
      does not use Router Discovery
    
      link-layer address: 
    
        preferred link-local ::1, life infinite
    
        preferred link-local fe80::1, life infinite
    
      link MTU 1500 (true link MTU 4294967295)
    
      current hop limit 128
    
      reachable time 44000ms (base 30000ms)
    
      retransmission interval 1000ms
    
      DAD transmits 0
    
      default site prefix length 48

    重新运行xp系统程序:

    可以使用下面代码过滤,取得ipv4地址或者ipv6地址:

                IPAddress address1 = Dns.GetHostAddresses(Dns.GetHostName()).Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
    
                IPAddress address2 = Dns.GetHostAddresses(Dns.GetHostName()).Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6).First();
  • 相关阅读:
    带你一分钟理解闭包js面向对象编程
    ThinkPad X1 Carbon 2022领衔,联想ThinkFamily 2022一众新品发布
    C#中const用法 C#中const用法详解
    ThinkPad哪个产品系列比较好?
    VB/VBA,可能超乎你的想象
    世界六大顶尖的软件强国
    2021年李宁跑鞋矩阵分析及挑选购买攻略
    .NET Core与.NET Framework、Mono之间的关系
    李宁动真格!多款「弹簧鞋」同时发售!扔地下能弹回来那种
    ts 泛型
  • 原文地址:https://www.cnblogs.com/gmth/p/3184351.html
Copyright © 2020-2023  润新知