使用 InetAddress 类通过 ip addr 查找主机名时出现问题

发布于 2024-10-10 23:18:33 字数 1445 浏览 0 评论 0原文

我写了两个程序 第一个 whois.java 查找给定主机名的 IP 地址

import java.net.*;
import java.io.*;

public class whois{
    public static void main(String args[]) throws IOException{
        String hostName = args[0];

        try{
            InetAddress ipaddress = InetAddress.getByName(hostName);
            System.out.println("IP address: " + ipaddress.getHostAddress());
        }catch(UnknownHostException e){
            System.out.println("Could not find IP address for: " + hostName);
        }
    }
}

,其他 whois2.java 查找给定 ip 的主机名

import java.net.*;
import java.io.*;

class whois2{
    public static void main(String args[]){
        try{

            String str[] = args[0].split("\\.");

            byte btArr[] = new byte[]{(byte)Integer.parseInt(str[0]), (byte)Integer.parseInt(str[1]), (byte)Integer.parseInt(str[2]), (byte)Integer.parseInt(str[3])};
            InetAddress ipAddr = InetAddress.getByAddress(btArr);
            System.out.println("Host name for this is : " + ipAddr.getHostName());
        }catch(UnknownHostException e){
            System.out.println("Unable to find the host for ip specified " + args[0]);
        }
    }
}

,然后我使用 jdk 1.6 运行该程序并得到以下输出:

$java whois google.com

IP address: 209.85.231.104

$java whois2 209.85.231.104

Host name for this is : maa03s01-in-f104.1e100.net

为什么主机名与 google.com 不同?

提前致谢

I have written the two program
1st whois.java to find the ip address of the given hostname

import java.net.*;
import java.io.*;

public class whois{
    public static void main(String args[]) throws IOException{
        String hostName = args[0];

        try{
            InetAddress ipaddress = InetAddress.getByName(hostName);
            System.out.println("IP address: " + ipaddress.getHostAddress());
        }catch(UnknownHostException e){
            System.out.println("Could not find IP address for: " + hostName);
        }
    }
}

and other whois2.java which finds hostname for given ip

import java.net.*;
import java.io.*;

class whois2{
    public static void main(String args[]){
        try{

            String str[] = args[0].split("\\.");

            byte btArr[] = new byte[]{(byte)Integer.parseInt(str[0]), (byte)Integer.parseInt(str[1]), (byte)Integer.parseInt(str[2]), (byte)Integer.parseInt(str[3])};
            InetAddress ipAddr = InetAddress.getByAddress(btArr);
            System.out.println("Host name for this is : " + ipAddr.getHostName());
        }catch(UnknownHostException e){
            System.out.println("Unable to find the host for ip specified " + args[0]);
        }
    }
}

and then i ran the program with jdk 1.6 and get following outputs:

$java whois google.com

IP address: 209.85.231.104

$java whois2 209.85.231.104

Host name for this is : maa03s01-in-f104.1e100.net

why the host name is different not google.com?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-10-17 23:18:33

由 DNS 查找定义,负责处理对特定主机名的请求的服务器不需要与原始查找具有相同的主机名。

一个更典型的例子是,对 foobar.com 的请求由 IP 上的服务器处理,该 IP 的主机名为 www.foobar.com。

另请注意,处理服务器可能因地区而异。

因此,我使用 linux host 工具得到相同的结果:

joel@bohr:~$ host google.com 
google.com has address 173.194.37.104

... 对 google.com 的请求应该由 173.194.37.104 的服务器处理

joel@bohr:~$ host 173.194.37.104
104.37.194.173.in-addr.arpa domain name pointer lhr14s02-in-f104.1e100.net.

... 173.194.37.104 的服务器的主机名是 lhr14s02 -in-f104.1e100.net

joel@bohr:~$ host lhr14s02-in-f104.1e100.net
lhr14s02-in-f104.1e100.net has address 173.194.37.104

...和健全性检查,lhr14s02-in-f104.1e100.net的IP确实是173.194.37.104

The server responsible, as defined by the DNS lookup, for handling requests to a particular hostname need not have the same hostname as that of the original lookup.

A more typical example would be that requests for foobar.com are handled by a server at an IP, with IP having hostname www.foobar.com.

Note also that the handling server may vary by region.

So I get the same using the linux host tool:

joel@bohr:~$ host google.com 
google.com has address 173.194.37.104

... requests to google.com should be handled by the server at 173.194.37.104

joel@bohr:~$ host 173.194.37.104
104.37.194.173.in-addr.arpa domain name pointer lhr14s02-in-f104.1e100.net.

... the hostname of the server at 173.194.37.104 is lhr14s02-in-f104.1e100.net

joel@bohr:~$ host lhr14s02-in-f104.1e100.net
lhr14s02-in-f104.1e100.net has address 173.194.37.104

... and sanity check, the IP of lhr14s02-in-f104.1e100.net is indeed 173.194.37.104

雄赳赳气昂昂 2024-10-17 23:18:33

whois(ip) 解析为注册服务器的名称,而不是域名服务器上的条目。

当我们在网络上使用 whois 服务时,也会发生同样的情况:

http://whois.domaintools.com/google.com< /a> 解析为 IP 74.125.155.99 (来自我的位置!),http: //whois.domaintools.com/74.125.155.99 解析主机 px-in-f99.1e100.net (这又与您的结果不同)

whois(ip) resolves to the name of the registered server, not to an entry on a domain name server.

Same happens when we use whois services on the web:

http://whois.domaintools.com/google.com resolves to IP 74.125.155.99 (from my location!), http://whois.domaintools.com/74.125.155.99 resolves the host px-in-f99.1e100.net (which again is different from your results)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文