本文介绍了无法从getHostName获取主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用此方法获取主机名/计算机名称。不幸的是我只能获得localhost而不能获得其他计算机。
I am trying to get hostname/computer name using this method. Unfortunately i only can get localhost but not other computer.
private String getHostName(String _strIP) { try { InetAddress inetAddress = InetAddress.getByName(_strIP); System.out.println("getHostAddress : " + inetAddress.getHostAddress()); System.out.println("getHostName : " + inetAddress.getHostName()); System.out.println("getCanonicalHostName : " + inetAddress.getCanonicalHostName()); return inetAddress.getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); } return strDefaultHostName; }结果(不是localhost)
the result (not localhost)
getHostAddress : 192.168.2.139 getHostName : 192.168.2.139 getCanonicalHostName : 192.168.2.139结果(localhost)
the result (localhost)
getHostAddress : 127.0.0.1 getHostName : localhost getCanonicalHostName : localhost谢谢
推荐答案您的DNS已损坏。然后返回IP号码。
Your DNS is broken. Then IP-numbers are returned instead.
更多推荐
无法从getHostName获取主机名
发布评论