Get IP address using JAVA program

Source: Internet
Author: User
Use a JAVA program to obtain the IP address-general Linux technology-Linux programming and kernel information. The following is a detailed description. When using the TCP/IP Internet, you often need to query the IP address of your host and the IP address of the www server. Although we can use IPCONFIG and PING to Query IP addresses, if we use this command in an application or APPLET, we will destroy our application interface.

Therefore, a simple program using JAVA can directly query the IP address of the host and the IP address of the www server.

// The file name is NetTool. java.

(Note: In the JAVA language, uppercase and lowercase are sensitive)

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 ());

}

// Obtain the IP address of LOCALHOST

Public InetAddress getMyIP (){

Try {myIPaddress = InetAddress. getLocalHost ();}

Catch (UnknownHostException e ){}

Return (myIPaddress );}

// Obtain the IP address of www.abc.com

Public InetAddress getServerIP (){

Try {myServer = InetAddress. getByName (

"Www.abc.com ");}

Catch (UnknownHostException e ){}

Return (myServer );}}

Because of the cross-platform features of JAVA, the above programs can be directly run on any machine with a JVM system installed after compilation. The above program aims to inspire you to convert the above Code into an APPLET and add it to your homepage, or write the address query result to a file to create your own local hosts file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.