网络使用JAVA从网络上的IP地址获取主机名
我正在努力找出我网络中所有连接客户端的信息。我能够找到客户(可以联系到),但无法获取有关他的信息。我只需要主机名(计算机名),但下面的代码只打印IP地址。我错过了什么
来源
private void getListOfHost(){
String subnet="192.168.0";
int timeout=100;
for (int i=2;i<15;i++){
String host=subnet + "." + i;
try {
if (InetAddress.getByName(host).isReachable(timeout)) {
InetAddress address = InetAddress.getByName(host);
System.out.println(host + " is reachable");
System.out.println("Canonical host: " + address.getCanonicalHostName());
System.out.print("Host name: " + address.getHostName() + "\n");
}
}
catch(Exception e){e.printStackTrace();}
}
}
输出
192.168.0.6 is reachable
Canonical host: 192.168.0.6
Host name: 192.168.0.6
# 1 楼答案
代码中没有遗漏任何内容,但错误源是被调用系统的安全管理器/防火墙
甲骨文InetAddress.getCanonicalHostname()的文档指出:
返回:
甲骨文InetAddress.getHostname()的文档中指出:
返回:
解决方案:-
无论您想查询哪个系统的主机名,只需禁用该系统的防火墙即可。安全经理将始终阻止有关网络中其他系统的系统特定查询,因为这被认为不是要共享的信息(不安全)你应该时刻记住这件事