在tcp/ip 互联网时,经常会需要查询自己主机的ip地址和www服务器的ip地址。虽然,我们可以使用ipconfig 和ping 进行ip地址查询,但是如果在应用程序或applet中使用此命令回破坏我们应用程序界面。
为此本人使用java做了一个简单的程序可以直接查询自己主机的ip地址和www服务器的ip地址。
// 文件名为 nettool.java (注意:在java 语言中大小写敏感)
import java.net.*;
public class nettool{
inetaddress myipaddress=null;
inetaddress myserver=null;
public static void main( string args[]){
nettool mytool;
mytool=new nettool();
system.out.println("your host ip is: " + mytool.getmyip());
system.out.println("the server ip is :" +mytool.getserverip());
}
//取得localhost的ip地址
public inetaddress getmyip() {
try { myipaddress=inetaddress.getlocalhost();}
catch (unknownhostexception e) {}
return (myipaddress);
}
//取得 www.abc.com 的ip地址
public inetaddress getserverip(){
try {myserver=inetaddress.getbyname("www.abc.com");}
catch (unknownhostexception e) {}
return (myserver);
}
}
闽公网安备 35060202000074号