根据IP地址解析出域名(40分)

  • 主题发起人 主题发起人 hongsen
  • 开始时间 开始时间
H

hongsen

Unregistered / Unconfirmed
GUEST, unregistred user!
如何用JAVA实现?
 
/* begin
ning Networking in Java - Chapter 8 */
import java.net.InetAddress;
/**
* A command-line utility for performing regular and reverse DNS lookups
*/
public class DNSResolver {
/**
* Expects single argument containing ado
main name ordo
tted decimal
* IP address and outputs thedo
main-address mappings.
*/
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: DNSResolver [ <name> | <address> ]");
System.exit(1);
}
// Determine if argument is an IP address or ado
main name: Since
//do
main names cannot start with a number, check for that.
boolean isReverseLookup = Character.isDigit(args[0].charAt(0));
try {
InetAddress[] addresses = InetAddress.getAllByName(args[0]);
for(int a=0;
a<addresses.length;
a++) {
InetAddress address = addresses[a];
if (isReverseLookup) {
if (address.getHostAddress().equals(address.getHostName())) {
System.out.println("Could not reverse resolve " +
address.getHostAddress());
} else
{
System.out.println(address.getHostAddress() +
" reverse resolves to " +
address.getHostName());
}
} else
{
System.out.println(address.getHostName() +
" resolves to " +
address.getHostAddress());
}
}
} catch (java.net.UnknownHostException e) {
System.err.println("Cannot resolve " + args[0]);
System.exit(1);
} catch (java.lang.SecurityException e) {
System.err.println("A security manager has been installed and the" +
" 'resolve' java.net.SocketPermission has not" +
" been granted");
System.exit(1);
}
}
}
 
我试了,没有用。用www.263.net测试一下就知道了。
 
后退
顶部