好吧,所以我调整了http://enigma2eureka.blogspot.com/2009/08/finding-your-ip-v4-broadcast-address.html的代码,试图在我的Wi-Fi路由器上找到广播地址的IP地址。
protected static InetAddress getBroadcastAddress() throws SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
System.out.println(networkInterface.toString() + networkInterface.getInterfaceAddresses());
if (networkInterface.isLoopback())
continue; // Don't want to broadcast to the loopback interface
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null)
continue;
return broadcast;
}
}
return null;
}
但是,它会打印以下内容 - 接口地址的名称和列表:
name:lo (Software Loopback Interface 1)[/127.0.0.1/8 [/127.255.255.255]]
name:eth0 (Microsoft Kernel Debug Network Adapter)[]
name:net0 (Belkin USB Wireless Adaptor)[null]
...
贝尔金适配器 - net0 - 似乎有一个空的广播地址,虽然它不应该。 (我记得设置了首选的IPv4系统属性)任何人都可以确定它返回null的原因吗?