【发布时间】:2026-01-16 11:10:02
【问题描述】:
我正在写作,因为我在为自己的 PC 找到正确的子网掩码时遇到了一些问题。 我已经阅读了How to get subnet mask of local system using java? 的问题,但如果我尝试:
InetAddress thiscomputer = InetAddress.getLocalHost();
NetworkInterface thisNetworkInterface = NetworkInterface.getByInetAddress(thiscomputer);
int thiscomputerSubnetMask = thisNetworkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
System.out.println("This pc subnetmask: "+thiscomputerSubnetMask);
它会写 64。thisNetworkInterface.getInterfaceAddresses() 对象只有一个元素,它是 128。
现在,我正在寻找一个可以在 ipv4 协议中使用的数字,而我的实际子网掩码是 255.255.255.240,所以我正在寻找 16(256-240),但我不能从我知道的方法中得到它。
我什至不明白 64 或 128 可能代表什么! 谁能帮帮我?
【问题讨论】:
-
也许你得到了 ipv6 的东西..+ 你没有使用同一个类