[J2SE] Use Java to obtain an IP address

Source: Internet
Author: User
Tags command line net domain domain name nslookup
IP address |j2se How to use Java to obtain the IP address of a domain name? The class that provides this functionality is called java.net.InetAddress. Let's assume that there is a domain name that uses a static getbyname to regain a inetaddress and then get an IP address that can be read out. The following code is a very basic command line.

Import java.net.InetAddress;
Import java.net.UnknownHostException;
public class NsLookup {
static public void Main (string[] args) {
try {
InetAddress address = Inetaddress.getbyname (args[0]);
System.out.println (args[0]+ "
: "+address.gethostaddress ());
}
catch (Unknownhostexception Uhe) {
System.err.println ("Unable to find:" +args[0]);
}
}
}

InetAddress can also obtain an IP address by using getaddress (), but its return value is an array of 4 bytes. Therefore, although getaddress () is useful in obtaining IP, it is not suitable for output.

The output of the command above looks like this:

bash$ Java code. NsLookup www.sun.com
www.sun.com:192.18.97.241

Sometimes a domain name can contain more than one IP address, such as a Microsoft Web server, to maintain load balancing. InetAddress provides a way to get all the IP addresses of a domain name. Let's consider the following code:

Import java.net.InetAddress;
Import java.net.UnknownHostException;
public class NsLookup {
static public void Main (string[] args) {
try {
String name = Args[0];
Inetaddress[] addresses = inetaddress.getallbyname (name);
for (int i=0; i<addresses.length; i++) {
System.out.println (name+ "[" +i+ "]
: "+address.gethostaddress ());
}
catch (Unknownhostexception Uhe) {
System.err.println ("Unable to find:" +args[0]);
}
}
}
For www.sun.com, the output will be:

bash$ Java code. NsLookup www.sun.com
Www.sun.com[0]: 192.18.97.241
However, for www.microsoft.com, the output will be:

bash$ Javad Code/nslookup.java
www.microsoft.com
Www.microsoft.com[0]: 207.46.230.218
WWW.MICROSOFT.COM[1]: 207.46.197.101
WWW.MICROSOFT.COM[2]: 207.46.230.229
WWW.MICROSOFT.COM[3]: 207.46.197.113
WWW.MICROSOFT.COM[4]: 207.46.230.219
WWW.MICROSOFT.COM[5]: 207.46.230.220
WWW.MICROSOFT.COM[6]: 207.46.197.102

Since Inetaddress,localhost must be handled in a special order. If the string "localhost" goes directly to the original version of the Nslookup program, you get the following rather useless result:

bash$ Java code. NsLookup www.sun.com
localhost:127.0.0.1
We can manually find the local address:

try {
inetaddress localhost = inetaddress.getlocalhost ();
System.out.println
("localhost:" +localhost.gethostaddress ());
System.out.println
("localhost:" +localhost.gethostname ());
catch (Unknownhostexception Uhe) {
System.err.println
("Localhost not seeable.") Something is odd. ");
}
Here is an example of an output:

localhost:192.168.13.15
Localhost:crab
The domain name of the local host does not return the full domain name of the machine, because it depends on the setting of the machine.

Finally, inetaddress may be used to turn IP addresses into domain names for these addresses, which is useful for analyzing web logs. InetAddress makes it easy for developers to deal with domain names, IP addresses, and enable them to interact with DNS servers.



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.