Java Network Programming from entry to mastery (5): Use the getHostName method of the InetAddress class to obtain the Domain Name

Source: Internet
Author: User
Tags domain name lookup

This method can obtain the Domain Name of the remote host or the local name. The getHostName method is defined as follows:

Public String getHostName ()
The following are three ways to create an InetAddress object. In these three ways, the values returned by getHostName are different.

1. Use the getLocalHost method to create an InetAddress object

If the InetAddress object is created using the getLocalHost method, getHostName returns the local name. The following code is used:


InetAddress address = InetAddress. getLocalHost ();
System. out. println (address. getHostName (); // output the local name.


2. Use a domain name to create an InetAddress object

After calling these two methods using the domain name as the parameter of the getByName and getAllByName methods, the system will automatically remember the domain name. When you call the getHostName method, you do not need to access the DNS server, but directly return this domain name. The following code is used:

InetAddress address = InetAddress. getByName ("www.oracle.com ");
System. out. println (address. getHostName (); // No need to access the DNS server, directly return the Domain Name

3. Use an IP address to create an InetAddress object

When using an IP address to create an InetAddress object (the getByName, getAllByName, and getByAddress methods can all create an InetAddress object through an IP address), you do not need to access the DNS server. Therefore, the getHostName method is used to search for a domain name on the DNS server. If the IP address does not exist or the DNS server does not allow ing between the IP address and the domain name, The getHostName method returns the IP address directly. The following code is used:


InetAddress address = InetAddress. getByName ("141.146.8.66 ");
System. out. println (address. getHostName (); // You must access the DNS server to obtain the domain name.
InetAddress address = InetAddress. getByName ("1.2.3.4"); // the IP address does not exist.
System. out. println (address. getHostName (); // the IP address is returned directly.

The preceding three cases show that the DNS server is accessed only when the getHostName method is called by the InetAddress object created by using the IP address. In other cases, the getHostName method does not access the DNS server, but directly returns the domain name or local name. The following code demonstrates how to use the getHostName method in different situations and calculate the number of milliseconds required in various situations.


Package mynet;

Import java.net .*;

Public class DomainName
{
Public static void main (String [] args) throws Exception
{
Long time = 0;
// Obtain the local name
InetAddress address1 = InetAddress. getLocalHost ();
System. out. println ("local name:" + address1.getHostName ());
// Directly return the Domain Name
InetAddress address2 = InetAddress. getByName ("www.oracle.com ");
Time = System. currentTimeMillis ();
System. out. print ("directly obtain the domain name:" + address2.getHostName ());
System. out. println ("time used :"
+ String. valueOf (System. currentTimeMillis ()-time) + "millisecond ");
// Search for a domain name through DNS
InetAddress address3 = InetAddress. getByName ("141.146.8.66 ");
System. out. println ("address3:" + address3); // The domain name is empty.
Time = System. currentTimeMillis ();
System. out. print ("Search for domain names through DNS:" + address3.getHostName ());
System. out. println ("time used:" + String. valueOf (System. currentTimeMillis ()-time) + "millisecond ");
System. out. println ("address3:" + address3); // outputs both the domain name and IP address.
}
}

Running result:


Local name: ComputerName
Get the Domain Name: www.oracle.com time: 0 ms
Address3:/141.146.8.66
Domain Name Lookup through DNS: bigip-otn-portal.oracle.com time: 92 ms
Address3: bigip-otn-portal.oracle.com/141.146.8.66


From the running results, we can see that the first millisecond is 0, and the second millisecond is 92. In this case, the InetAddress object created using the domain name does not access the DNS server when using the getHostName method, And the InetAddress object created using the IP address needs to access the DNS server when using the getHostName method. For the third millisecond value, it may become smaller and smaller after multiple running of DomainName, because of the DNS server cache. However, this number is generally greater than 0. Maybe there are a lot of people will ask, the second line and the fourth line to get the domain name is not the same, in fact, www.oracle.com and bigip-otn-portal.oracle.com are oracle domain name, we can also through http: // bigip-otn-portal.oracle.com to visit the official oracle website. The differences between these two domain names will be discussed in the following article

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.