Use the Java program to get the IP address

zhaozj2021-02-17  75

Use the Java program to get the IP address

Li Luqun

-------------------------------------------------- ------------------------------ When you have a TCP / IP Internet, you often need to query your host's IP address and WWW server. IP address. Although we can use ipconfig and ping for IP address queries, but if you use this command back to the application or applet to destroy our application interface. To this end, I have a simple program that uses Java to directly query my host's IP address and the IP address of the WWW server.

// File name is Nettool.java (Note: 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 ());

// Get the IP address LOCALHOST public InetAddress getMyIP () {try {myIPaddress = InetAddress.getLocalHost ();} catch (UnknownHostException e) {} return (myIPaddress);} // IP address acquired public InetAddress www.abc.com GetServerip () {try {myserver = inetaddress.getbyname ("www.abc.com");} catch (unknownhostException e) {} return (myserver);

} Due to the cross-platform characteristics of the Java language, the above program can be run directly on any machine with a JVM system. The above procedures are intended to throw bricks, readers can transform the above code into applets to add to your homepage, or write the results of the address query to a file to build your local HOSTS file.

转载请注明原文地址:https://www.9cbs.com/read-31204.html

New Post(0)