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.
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:
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.
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.