欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA获得域名IP地址的方法

程序员文章站 2024-03-04 09:11:35
本文实例讲述了java获得域名ip地址的方法。分享给大家供大家参考。具体如下: import java.net.inetaddress; import java...

本文实例讲述了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.jb51.net;
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);
}
}

希望本文所述对大家的java程序设计有所帮助。