在TCP/IP 互联网时,经常会需要查询自己主机的IP地址和www服务器的IP地址
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);
}
}
博客介绍了在TCP/IP互联网环境下,使用Java代码查询主机和www服务器IP地址的方法。通过`InetAddress`类,分别获取本地主机和指定服务器(如www.abc.com)的IP地址,并将结果输出。

1万+

被折叠的 条评论
为什么被折叠?



