getNetworkPrefixLength() 返回奇怪的子网掩码

发布于 2024-11-19 10:06:45 字数 474 浏览 3 评论 0原文

以下代码:

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    System.out.println(address.getAddress() + "/" + address.getNetworkPrefixLength());
}

返回:

/fe80:0:0:0:11da:433a:412a:8c23%13/64
/192.168.1.107/128

我的子网掩码是 /24,而不是 /128。这是Java错误吗?

The following code:

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    System.out.println(address.getAddress() + "/" + address.getNetworkPrefixLength());
}

returns:

/fe80:0:0:0:11da:433a:412a:8c23%13/64
/192.168.1.107/128

My subnet mask is /24, not /128. Is it a Java bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光暖心i 2024-11-26 10:06:45

也许您遇到了这个错误: https://bugs.java.com/bugdatabase/view_bug? bug_id=6707289

(请停止投票...这只有 1 个谷歌距离...)

Maybe you hit this bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=6707289

(Please stop to upvote ... this was only 1 google away ...)

空‖城人不在 2024-11-26 10:06:45

至少对我来说没有任何意义... /128 对于 IP4 网络无效(/32 应该是最高 em>(也是最没用的;))用单个地址来评估和描述网络,例如网络掩码255.255.255.255)。

/128 对于 IP6 来说是相同的,这是一种只有一个地址的网络(= 128 位固定前缀)。

At least it doesn't make any sense to me... /128 can't be valid for IP4 networks (/32 should be the highest (and most useless ;) ) value and describe a network with a single address, like a network mask 255.255.255.255).

/128 would be the same for IP6, a network with just one address (= a 128 bit fixed prefix).

似狗非友 2024-11-26 10:06:45

添加到信息库,通过下面的 ipconfig 输出,我在 jdk 1.8/windows 8.1 上得到一些奇怪的前缀长度:

length and ip4 interface address: 24, /192.168.1.104/24 [/192.168.1.255]
length and strange interface address: 64, /fe80:0:0:0:a162:16a3:1dc7:4f98%eth1/64 [null]
length and ip6 interface address: 128, /fe80:0:0:0:0:5efe:c0a8:168%net0/128 [null]
length and ip6 interface address: 128, /fe80:0:0:0:0:5efe:c0a8:12%net1/128 [null]
length and strange interface address: 64, /fe80:0:0:0:8190:5432:56a8:c006%wlan2/64 [null]
length and strange interface address: 64, /fe80:0:0:0:191c:bc6:985d:c0be%wlan3/64 [null]
length and strange interface address: 64, /fe80:0:0:0:29f9:1ede:602f:4530%wlan4/64 [null]
length and strange interface address: 0, /2001:0:5ef5:79fb:3c9f:4a:b354:8e66/0 [null]
length and strange interface address: 32, /fe80:0:0:0:3c9f:4a:b354:8e66%net2/32 [null]
length and strange interface address: 64, /fe80:0:0:0:181f:6633:5212:2f82%eth2/64 [null]
length and strange interface address: 64, /fe80:0:0:0:5df5:554b:5188:d070%eth3/64 [null]
length and ip4 interface address: 24, /192.168.0.18/24 [/192.168.0.255]
length and strange interface address: 64, /fe80:0:0:0:e828:e682:6a93:296d%wlan5/64 [null]



Windows IP Configuration


Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : 
   Link-local IPv6 Address . . . . . : fe80::e828:e682:6a93:296d%14
   IPv4 Address. . . . . . . . . . . : 192.168.0.18
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

Ethernet adapter Ethernet 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Ethernet adapter Ethernet 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Local Area Connection* 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Local Area Connection* 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Wi-Fi 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : socal.rr.com
   Link-local IPv6 Address . . . . . : fe80::a162:16a3:1dc7:4f98%3
   IPv4 Address. . . . . . . . . . . : 192.168.1.104
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Tunnel adapter isatap.{3ABD909F-0B6E-4DDB-AF74-1B3A98B48A56}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter isatap.socal.rr.com:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : socal.rr.com

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Connection-specific DNS Suffix  . : 
   IPv6 Address. . . . . . . . . . . : 2001:0:5ef5:79fb:3c9f:4a:b354:8e66
   Link-local IPv6 Address . . . . . : fe80::3c9f:4a:b354:8e66%11
   Default Gateway . . . . . . . . . : ::

adding to the information base, with the ipconfig output below, i get some strange prexix lengths on jdk 1.8/windows 8.1:

length and ip4 interface address: 24, /192.168.1.104/24 [/192.168.1.255]
length and strange interface address: 64, /fe80:0:0:0:a162:16a3:1dc7:4f98%eth1/64 [null]
length and ip6 interface address: 128, /fe80:0:0:0:0:5efe:c0a8:168%net0/128 [null]
length and ip6 interface address: 128, /fe80:0:0:0:0:5efe:c0a8:12%net1/128 [null]
length and strange interface address: 64, /fe80:0:0:0:8190:5432:56a8:c006%wlan2/64 [null]
length and strange interface address: 64, /fe80:0:0:0:191c:bc6:985d:c0be%wlan3/64 [null]
length and strange interface address: 64, /fe80:0:0:0:29f9:1ede:602f:4530%wlan4/64 [null]
length and strange interface address: 0, /2001:0:5ef5:79fb:3c9f:4a:b354:8e66/0 [null]
length and strange interface address: 32, /fe80:0:0:0:3c9f:4a:b354:8e66%net2/32 [null]
length and strange interface address: 64, /fe80:0:0:0:181f:6633:5212:2f82%eth2/64 [null]
length and strange interface address: 64, /fe80:0:0:0:5df5:554b:5188:d070%eth3/64 [null]
length and ip4 interface address: 24, /192.168.0.18/24 [/192.168.0.255]
length and strange interface address: 64, /fe80:0:0:0:e828:e682:6a93:296d%wlan5/64 [null]



Windows IP Configuration


Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : 
   Link-local IPv6 Address . . . . . : fe80::e828:e682:6a93:296d%14
   IPv4 Address. . . . . . . . . . . : 192.168.0.18
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

Ethernet adapter Ethernet 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Ethernet adapter Ethernet 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Local Area Connection* 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Local Area Connection* 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Wi-Fi 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : socal.rr.com
   Link-local IPv6 Address . . . . . : fe80::a162:16a3:1dc7:4f98%3
   IPv4 Address. . . . . . . . . . . : 192.168.1.104
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Tunnel adapter isatap.{3ABD909F-0B6E-4DDB-AF74-1B3A98B48A56}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter isatap.socal.rr.com:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : socal.rr.com

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Connection-specific DNS Suffix  . : 
   IPv6 Address. . . . . . . . . . . : 2001:0:5ef5:79fb:3c9f:4a:b354:8e66
   Link-local IPv6 Address . . . . . : fe80::3c9f:4a:b354:8e66%11
   Default Gateway . . . . . . . . . : ::
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文