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. 如何用Java實(shí)現(xiàn)排列組合算法2. Java 接口和抽象類的區(qū)別詳解3. Java JUC中操作List安全類的集合案例4. 詳解Java的編譯執(zhí)行與解釋執(zhí)行5. JavaScript中數(shù)組與字符串的includes()用法6. Python基于pyjnius庫實(shí)現(xiàn)訪問java類7. java二維數(shù)組指定不同長度實(shí)例方法8. Java+Swing實(shí)現(xiàn)醫(yī)院管理系統(tǒng)的完整代碼9. 解決Java中的java.io.IOException: Broken pipe問題10. 如何用Java注解和反射實(shí)現(xiàn)依賴注入
