Java Network Programming from entry to mastery (6): Use the getCanonicalHostName method to obtain the Host Name

Source: Internet
Author: User

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 example 3-8 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 through www.ibm.com
Host Name: www.ibm.com
Host alias: www.ibm.com

Create an InetAddress object through www.126.com
Master name: zz-9-77-a8.bta.net.cn
Host alias: www.126.com

Create an InetAddress object through 202.108.9.77
Master name: zz-9-77-a8.bta.net.cn
Host alias: zz-9-77-a8.bta.net.cn

Create an InetAddress object through 211.100.26.121
Master machine 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.