JAVA獲得域名的IP地址
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
InetAddress myIpAddress = null;
InetAddress[] myServer = null;
public static void main(String args[]) {
TestInetAddress address = new TestInetAddress();
System.out.println("Your host IP is: " + address.getLocalhostIP());
String domain = "www.163.com";
System.out.println("The server domain name is: " + domain);
InetAddress[] array = address.getServerIP(domain);
int count=0;
for(int i=1; i<array.length; i++){
System.out.println("ip "+ i +" "+ address.getServerIP(domain)[i-1]);
count++;
}
System.out.println("IP address total: "+count);
}
/**
* 獲得 localhost 的IP地址
* @return
*/
public InetAddress getLocalhostIP() {
try {
myIpAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return (myIpAddress);
}
/**
* 獲得某域名的IP地址
* @param domain 域名
* @return
*/
public InetAddress[] getServerIP(String domain) {
try {
myServer = InetAddress.getAllByName(domain);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return (myServer);
}
}
相關(guān)文章:
1. JavaScript Tab菜單實(shí)現(xiàn)過程解析2. 詳解Java 虛擬機(jī)垃圾收集機(jī)制3. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)4. javascript設(shè)計(jì)模式 ? 建造者模式原理與應(yīng)用實(shí)例分析5. JavaScrip簡(jiǎn)單數(shù)據(jù)類型隱式轉(zhuǎn)換的實(shí)現(xiàn)6. JavaScript中clientWidth,offsetWidth,scrollWidth的區(qū)別7. java 如何讀取properties文件8. 用Java實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP)9. asp+jsp+JavaScript動(dòng)態(tài)實(shí)現(xiàn)添加數(shù)據(jù)行10. Java并發(fā)編程之CountDownLatch源碼解析
