Java Programming gets IP address

Source: Internet
Author: User

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.

Related Article

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.