① java怎么通过域名获取ip地址
importjava.net.InetAddress;
importjava.net.UnknownHostException;
publicclassTestInetAddress{
InetAddressmyIpAddress=null;
InetAddress[]myServer=null;
publicstaticvoidmain(Stringargs[]){
TestInetAddressaddress=newTestInetAddress();
System.out.println("YourhostIPis:"+address.getLocalhostIP());
Stringdomain=www.jb51.net;
System.out.println("Theserverdomainnameis:"+domain);
InetAddress[]array=address.getServerIP(domain);
intcount=0;
for(inti=1;i<array.length;i++){
System.out.println("ip"+i+""+address.getServerIP(domain)[i-1]);
count++;
}
System.out.println("IPaddresstotal:"+count);
}
/**
*获得localhost的IP地址
*@return
*/
(){
try{
myIpAddress=InetAddress.getLocalHost();
}catch(UnknownHostExceptione){
e.printStackTrace();
}
return(myIpAddress);
}
/**
*获得某域名的IP地址
*@paramdomain域名
*@return
*/
publicInetAddress[]getServerIP(Stringdomain){
try{
myServer=InetAddress.getAllByName(domain);
}catch(UnknownHostExceptione){
e.printStackTrace();
}
return(myServer);
}
}