日期:2014-05-20  浏览次数:20843 次

如何获得域名 主机名 的IP地址
如题

------解决方案--------------------
import java.net.InetAddress;
import java.net.UnknownHostException;
  public class NsLookup {
   static public void main(String[] args) {
   try {
  InetAddress address = InetAddress.getByName(args[0]);
  System.out.println(args[0]+“
  : “+address.getHostAddress());
   }
   catch(UnknownHostException uhe) {
  System.err.println(“Unable to find: “+args[0]);
   }
这个是用控制台的 在控制台输入 主机名 或域名 显示IP地址 测试过能用的
------解决方案--------------------
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DNSLookup {
public static void main(String args[]) {
try {
InetAddress host;
if (args.length == 0) {
host = InetAddress.getLocalHost();
} else {
host = InetAddress.getByName(args[0]);
}
System.out.println( "Host: ' " + host.getHostName()
+ " ' has address: " + host.getHostAddress());
byte bytes[] = host.getAddress();
int fourBytes[] = new int[bytes.length];
for (int i = 0, n = bytes.length; i < n; i++) {
fourBytes[i] = bytes[i] & 255;
}
System.out.println( "\t " + fourBytes[0] + ". " + fourBytes[1] + ". "
+ fourBytes[2] + ". " + fourBytes[3]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

------解决方案--------------------
import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIP {
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getByName( "http://localhost:8080 ");
} catch (UnknownHostException e) {
System.exit(2);
}
System.out.println(address.getHostName() + "= "
+ address.getHostAddress());
System.exit(0);
}
}