使用 InetAddress 类通过 ip addr 查找主机名时出现问题
我写了两个程序 第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由 DNS 查找定义,负责处理对特定主机名的请求的服务器不需要与原始查找具有相同的主机名。
一个更典型的例子是,对 foobar.com 的请求由 IP 上的服务器处理,该 IP 的主机名为 www.foobar.com。
另请注意,处理服务器可能因地区而异。
因此,我使用 linux host 工具得到相同的结果:
... 对 google.com 的请求应该由 173.194.37.104 的服务器处理
... 173.194.37.104 的服务器的主机名是 lhr14s02 -in-f104.1e100.net
...和健全性检查,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:
... requests to google.com should be handled by the server at 173.194.37.104
... the hostname of the server at 173.194.37.104 is lhr14s02-in-f104.1e100.net
... and sanity check, the IP of lhr14s02-in-f104.1e100.net is indeed 173.194.37.104
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 hostpx-in-f99.1e100.net
(which again is different from your results)