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);
}
}
相关文章:
1. JavaScript实现组件化和模块化方法详解2. Javaweb工程运行报错HTTP Status 404解决办法3. JavaScript使用Promise封装Axios进行高效开发4. 基于javascript原生判断DOM是否加载完毕5. Java Parallel Processing Framework 0.15.0 发布6. 如何使用JCTools实现Java并发程序7. Java中避免过多if-else的几种方法8. EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常9. vue前端RSA加密java后端解密的方法实现10. java实现简易贪吃蛇游戏