Java network programming from getting started to mastering (5): Obtaining a domain name using the GetHostName method of the InetAddress class

Source: Internet
Author: User
Tags to domain

This method can get the domain name of the remote host, and can also get the native name . The GetHostName method is defined as follows:

Public String gethostname ()

Here are three ways to create a InetAddress object, and in these three ways, the value returned by GetHostName is different.

1 . to create a inetaddress object using the getlocalhost method

If the InetAddress object was created with the Getlocalhost method, GetHostName returns the native name . As shown in the following code:

InetAddress address = Inetaddress.getlocalhost ();  System.out.println (Address.gethostname ()); Output native name

2 . Create a inetaddress object using a domain name

This domain name is automatically remembered when you call both methods with the domain name as a parameter to the Getbyname and Getallbyname methods. When you call the GetHostName method, you do not need to access the DNS server again, but instead return the domain name directly. as shown in the following code:

InetAddress address = Inetaddress.getbyname ("www.oracle.com");  System.out.println (Address.gethostname ()); No need to access DNS server, directly return domain name

3 . Create a inetaddress object with an IP address

When you create a InetAddress object using an IP address (the Getbyname, Getallbyname, and getbyaddress methods can all create InetAddress objects by IP address), you do not need to access the DNS server. Therefore, the work of finding a domain name through a DNS server is done by the GetHostName method. If this IP address does not exist or the DNS server does not allow the mapping of IP addresses and domain names, the GetHostName method returns this IP address directly. As shown in the following code:

InetAddress address = Inetaddress.getbyname ("141.146.8.66");  System.out.println (Address.gethostname ());  Need to access the DNS server to get the domain name inetaddress address = Inetaddress.getbyname ("1.2.3.4");  IP address does not exist System.out.println (Address.gethostname ()); Return IP address directly

As can be seen from the three scenarios above, 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 instead returns the domain name or the native name directly. The following code shows how to use the GetHostName method in different situations and calculates the number of milliseconds required for each situation.

PackageMyNet;      Import java.net.*; public class domainname   {      public static void main (String [] args]  throws exception       {           long time = 0;           //  get the name of the machine            inetaddress address1 = inetaddress.getlocalhost ();           system.out.println ("Native name: "  +  Address1.gethostname ());           //  direct return to 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:"                             + string.valueof (System.currenttimemillis ()  - time)  +   "  milliseconds");           //  find domain names through DNS            inetaddress address3 = inetaddress.getbyname ("141.146.8.66");           System.out.println ("address3:  "  +  ADDRESS3);  //  domain name is empty           time =  system.currenttimemillis ();           system.out.print ("Find Domain name:  through DNS"  +  Address3.gethostname ());           system.out.println ("   Time:"+ string.valueof (System.currenttimemillis ()-time) + "MS");  System.out.println ("ADDRESS3:" + ADDRESS3); Simultaneous output domain name and IP address} }

Operation Result:

Native name: ComputerName directly get domain name: www.oracle.com time: 0 ms ADDRESS3:/141.146.8.66 lookup domain name through DNS: bigip-otn-portal.oracle.com elapsed time: 92 Ms address3:bigip-otn-portal.oracle.com/141.146.8.66

As you can see from the running results above, the first number of milliseconds is 0, and the second number of milliseconds is 92. When this is said, the InetAddress object created with the domain name does not access the DNS server when using the GetHostName method, while the InetAddress object created with the IP address requires access to the DNS server when using the GetHostName method. For the third millisecond number, it is possible to run DomainName more than once, due to the caching of the DNS server. But in general this number will be greater than 0. Perhaps a lot of people will ask, the second row and the fourth row get the domain name how different, in fact www.oracle.com and bigip-otn-portal.oracle.com are Oracle domain name, we can also pass HTTP// Bigip-otn-portal.oracle.com to visit the official website of Oracle. The difference between these two domains will be discussed in the following article.

Java network programming from getting started to mastering (5): Obtaining a domain name using the GetHostName method of the InetAddress class

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.