2011-01-06 53 views
2

已经写两个程序 1 whois.java找到指定的主机名问题,利用InetAddress类

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地址,该主机名查找与IP地址查找主机名鉴于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?

在此先感谢

回答

2

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

一个更典型的例子是,对于foobar.com的请求由IP地址为IP的服务器处理,IP地址为www.foobar.com。

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

所以,我开始使用Linux的主机工具相同:

[email protected]:~$ host google.com 
google.com has address 173.194.37.104 

...请求到google.com应该由服务器在173.194.37.104

[email protected]:~$ 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

[email protected]:~$ 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

+0

不错,不知道这个可爱的小util! – asgs 2013-10-04 05:01:38