Не удается получить "Имя хоста" в локальной сети
InetAddress addr = InetAddress.getByName("192.168.1.3");
String host = addr.getHostAddress();
System.out.println(host);
InetAddress addr2 = InetAddress.getLocalHost();
System.out.println("Local HostAddress: "+addr2.getHostAddress());
String hostname = addr2.getHostName();
System.out.println("Local host name: "+hostname);
Пытаюсь получить имя устройства в локальной сети "смартфон андроид" по ip адресу но ничего не выходит. Второй же блок кода удачно выдает имя моего компьютера с которого запускаю код. Подскажите так ли это делается или же нужен иной подход?
Ответы (1 шт):
Согласно документации
If there is a security manager, its checkConnect method is first called with the hostname and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, it will return the textual representation of the IP address.
Returns: the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.
Вам вернется имя в случае отсутствия ограничений со стороны security manager.
try {
InetAddress addr = InetAddress.getByName("192.168.2.251");
String host = addr.getHostAddress();
System.out.println(host); //192.168.2.251
String hostname = addr.getHostName();
System.out.println("Local host name: " + hostname); //Local host name: WIN-USUUHLKQ03V
} catch (Exception e) {
// TODO: handle exception
}
попробовал так
try {
InetAddress addr = InetAddress.getByName("64.233.164.101");
String host = addr.getHostAddress();
System.out.println(host); // 64.233.164.101
String hostname = addr.getHostName();
System.out.println("Local host name: " + hostname); // Local host name: lf-in-f101.1e100.net
} catch (Exception e) {
// TODO: handle exception
}
что интересно в таком случае мне выдаёт
try {
InetAddress addr = InetAddress.getByName("192.168.2.100");
String host = addr.getHostAddress();
System.out.println(host); // 192.168.2.100
String hostname = addr.getHostName();
System.out.println("Local host name: " + hostname); // Local host name: 192.168.2.100
} catch (Exception e) {
// TODO: handle exception
}
и на случай не существующего адреса аналогичный результат.