How to obtain a domain name using the inetaddress class

Source: Internet
Author: User

1. gethostname ()


The gethostname () 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:

Netaddress Address = inetaddress. getbyname ("www.oracle.com"); system. Out. println (address. gethostname (); // The domain name is directly returned without access to the DNS server.

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 = inetaddress. getbyname ("1.2.3.4"); // the IP address does not exist in the system. out. println (address. gethostname (); // returns the IP address 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 ("Get domain name directly:" + address2.gethostname (); system. out. println ("time used:" + String. valueof (system. currenttimemillis ()-Time) + "millisecond"); // query the domain name inetaddress address3 = inetaddress through DNS. 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 directly get Domain Name: www.oracle.com time: 0 milliseconds address3:/141.146.8.66 Domain Name: bigip-otn-portal.oracle.com time: 92 milliseconds 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 of the domain name is not the same, in fact, www.oracle.com and bigip-otn-portal.oracle.com are the domain name of Oracle, we can also access the official website of Oracle through the http://bigip-otn-portal.oracle.com. The differences between these two domain names will be discussed below.

2. getcanonicalhostname ()

The getcanonicalhostname method is the same as the gethostname method to obtain the Domain Name of the remote host. But they have a difference. The host name obtained by getcanonicalhostname is the host alias obtained by gethostname. Getcanonicalhostname is defined as follows:

public String getCanonicalHostName()

When accessing some domain names, the getcanonicalhostname method returns the same value as the gethostname method. This is related to how the DNS server interprets host names and host aliases and their settings. For example, after creating an inetaddress object through www.ibm.com, The getcanonicalhostname method and the gethostname method both return the result www.ibm.com (sometimes the IP address is directly returned, which may be related to the processing mechanism of the ibm dns server ). If DNS does not allow domain names to be obtained through IP addresses, the two methods will return IP addresses instead of domain names. The getcanonicalhostname method can be discussed in three cases:

1. Use getlocalhost to create an inetaddress object

In this case, both the getcanonicalhostname method and the gethostname method obtain the local name.

2. Use a domain name to create an inetaddress object


In this case, whether the getcanonicalhostname method accesses the DNS server depends on how the DNS server interprets the Host Name and host alias. That is to say, whether the host name and host alias are determined when the inetaddress object is created. As mentioned earlier, after the domain name is used to create an inetaddress object, the gethostname method will not be called to access the DNS server. However, the getcanonicalhostname method is not required. This is related to the settings of the DNS server. For example, www.126.com needs to access the DNS server, but www.ibm.com does not need to access the DNS server.

3. Use an IP address to create an inetaddress object

In this case, the getcanonicalhostname method and the gethostname method are identical, that is, they all get the host name instead of the host alias.


The host alias is used because sometimes the host name may be more complex, such as the host name bigip-otn-portal.oracle.com of the Oracle official website, so in order to make the user access the website more convenient, it adds a simpler host alias, for example, www.oracle.com. A host name may correspond to multiple host aliases. For example, oracle.com is also the host alias of oracle. Enter http://bigip-otn-portal.oracle.com and http://oracle.com in the address bar of IE to visit the official Oracle website. However, we found that many websites cannot be accessed through host names and can only be accessed through some aliases. For example, 126 can only be accessed through two host aliases: www.126.com and 126.com, instead of accessing through its host name zz-9-77-a8.bta.net.cn. This is because the HTTP protocol is used on the server, which has been discussed earlier. The following compares the output results of the getcanonicalhostname and gethostname methods in different situations.

Package mynet; import java.net. *; public class domainname {public static void outhostname (inetaddress Address, string s) {system. out. println ("creating inetaddress object through" + S + "); system. out. println ("Master name:" + address. getcanonicalhostname (); system. out. println ("host alias:" + address. gethostname (); system. out. println ("");} public static void main (string [] ARGs) throws exception {outhostname (inetaddress. getlocalhost (), "getlocalhost method"); outhostname (inetaddress. getbyname ("www.ibm.com"), "www.ibm.com"); outhostname (inetaddress. getbyname ("www.126.com"), "www.126.com"); outhostname (inetaddress. getbyname ("202.108.9.77"), "202.108.9.77"); outhostname (inetaddress. getbyname ("211.100.26.121"), "211.100.26.121 ");}}

Running result:

Use the getlocalhost method to create an inetaddress object master machine name: computername host alias: computername create an inetaddress object master machine name: www.ibm.com host alias: www.ibm.com create an inetaddress object master machine name through www.126.com: zz-9-77-a8.bta.net.cn host alias: www.126.com create inetaddress object Host Name: zz-9-77-a8.bta.net.cn host alias: zz-9-77-a8.bta.net.cn create inetaddress object Host Name: 211.100.26.121 create inetaddress object Host Name: 211.100.26.121 host alias: 211.100.26.121

From the preceding running results, we can see that if the inetaddress object is created through an IP address, the values of the getcanonicalhostname method and the gethostname method are identical, and their values may be host names or IP addresses. The inetaddress objects created with domain names are not necessarily the same, and their values may be the same (the same IP address or domain name) or different, for example, in the preceding running result www.126.com, the values obtained by using these two methods are different. In general, we can use gethostname to obtain the domain name, because if the domain name is used to create the inetaddress object, the domain name obtained by gethostname is used to create the Domain Name of the inetaddress object, if you use an IP address to create an inetaddress object, the gethostname method is equivalent to the getcanonicalhostname method.

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.