无法从getHostName获取主机名 [英] Cannot get hostname from getHostName

查看:725
本文介绍了无法从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获取主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆