已经写两个程序 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?
在此先感谢
不错,不知道这个可爱的小util! – asgs 2013-10-04 05:01:38