【问题标题】:Inetaddress returning unexpected HostnameInetaddress 返回意外的主机名
【发布时间】:2013-11-26 04:32:02
【问题描述】:

我有下面的代码,在我继续之前,我使用它来获取服务器的完整规范主机名。 这是在我的服务器上返回一个值(特别是不同的域)。命令行上的 nslookup 返回正确的值。

我试图了解 java.inet 在内部究竟做了什么来解析正确的完整规范名称。 它是否查询 DNS 服务器?

import java.net.InetAddress;

public class IpLookup {

    public static void main(String[] args) {

        try{

            String REQUESTSERVER = args[0];  
            InetAddress in = InetAddress.getByName(REQUESTSERVER);
            REQUESTSERVER = in.getCanonicalHostName();
            System.out.println("Canonical REQUESTSERVER "+ REQUESTSERVER );
        } catch(Exception e) {
            System.out.println("lookup failed");
        }
    }
}

【问题讨论】:

    标签: java network-programming


    【解决方案1】:

    发生的情况是 nslookup 返回您输入的主机名及其 IP 地址,而 Java 代码首先查找 IP,然后根据返回的 IP 地址使用反向 DNS。 rDNS 可能未配置为返回您认为的“规范”地址。

    我在 www.google.com 上运行了 nslookup:

    > www.google.com
    Non-authoritative answer:
    Server:  perseus.jhmg.pvt
    Address:  192.168.10.254
    
    Name:    www.google.com
    Addresses:  2607:f8b0:400a:801::1010
              173.194.33.146
              173.194.33.144
              173.194.33.148
              173.194.33.147
              173.194.33.145
    

    然后我在www.google.com 上运行你的程序,得到了这个结果:

    Canonical REQUESTSERVER sea09s17-in-f17.1e100.net
    

    对 nslookup 返回的第一个地址进行反向 DNS 搜索:

    > 173.194.33.146
    Server:  perseus.jhmg.pvt
    Address:  192.168.10.254
    
    Name:    sea09s17-in-f18.1e100.net
    Address:  173.194.33.146
    

    (顺便说一句,注意 1e100 是 10100 或称为“googol”的数字...可爱:-)

    这证明了正在发生的事情。

    【讨论】:

    • 吉姆,感谢您的调查。我得到了答案。
    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多