In the TCP/IP Internet, you often need to query your host's IP address and the WWW server's IP address. Although we can use ipconfig and ping for IP address queries, using this command in an application or applet can compromise our application interface.
To this end I use Java to do a simple program can directly query their own host IP address and the IP address of the WWW server:
File name is Nettool.java (note: Case sensitive in the Java language)
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.bianceng.cn 的IP地址
public InetAddress getServerIP(){
try {myServer=InetAddress.getByName(
"www.bianceng.cn");}
catch (UnknownHostException e) {}
return (myServer);
}
}
----Because of the cross-platform nature of the Java language, the above program is compiled to run directly on any machine that has a JVM system installed. The above procedure aims to initiate, the reader may transform the above code slightly transforms into the applet adds to your homepage, or writes the address query result to a file, establishes own local hosts file.