TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
在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.sina.com 的IP地址
public InetAddress getServerIP(){
try {myServer=InetAddress.getByName("www.sina.com");}
catch (UnknownHostException e) {}
return (myServer);
}
}
运行结果:
C:java>java NetTool
Your host IP is: zz2zcwb/169.254.104.105
The Server IP is :www.sina.com/218.30.108.109
由于JAVA语言的跨平台特性,以上程序编译后可直接在任何装有JVM系统的机器上运行。以上程序旨在抛砖引玉,读者可将上述代码稍加变换转化成APPLET加到你的homepage中,或将地址查询结果写到一个文件中去,建立自己本地的hosts文件。
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
} |
|