我正在编写一个Java程序,该程序将显示连接到我的Wifi网络的设备的名称和IP地址。加速查找连接到Wifi网络的设备并获取设备名称
我想出了IP地址部分。这里的代码:
public static void main(String[] args) throws IOException {
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++) {
ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000)) {
// machine is turned on and can be pinged
System.out.println(address + "is online");
} else if (!address.getHostAddress().equals(address.getHostName())) {
// machine is known in a DNS lookup
System.out.println(address + "is in a DNS lookup");
} else {
// the host address and host name are equal, meaning the host name could not be resolved
System.out.println(address + " is not online");
}
}
}
此代码的工作原理,它显示连接的设备的IP地址。
但也有我面临两个问题:
- 我不能获得连接设备的名称。我只能得到IP地址。
- 这个程序工作起来非常慢。完成需要254秒。
那么,如何显示连接设备的名称,并有什么方法来加快这个程序?
任何帮助表示赞赏!
您假定您的网络是一个/ 24网络。可能有超过254个节点连接到您的WiFi网络,具体取决于子网掩码。 – RealSkeptic
BruceWayne的建议是获取设备名称吗? – afzalex
@afzalex没有。它没有工作。你知道一种获取设备名称的方法吗?请帮助我! –