JAVA获得域名IP地址的方法
本文实例讲述了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.nhooo.com; 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 */ publicInetAddressgetLocalhostIP(){ 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); } }
希望本文所述对大家的java程序设计有所帮助。