<< Chapter < Page Chapter >> Page >

One online description for a canonical host name reads as follows:

"A host machine on a network can be identified by several different names. However, each host must have one official hostname. All other hostnames are considered aliases of the canonical hostname"

Get and display canonical host name for Google

Returning now to Google, the code in Listing 5 gets and displays the canonical hostname for Google.

Listing 5 - Get and display canonical host name for Google.
System.out.println( "Display canonical host name for Google");//Get InetAddress containing one of Google's // IP addresses.address = InetAddress.getByName("www.google.com"); System.out.println(address.getCanonicalHostName());

The code in Listing 5 begins by getting a reference to one of the InetAddress objects that represent www.google.com . Then the getCanonicalHostName method is called on that object to get and display the canonical host name. The code in Listing 5 produces the output shown in Figure 5 .

Figure 5 - Canonical host name for Google.
Display canonical host name for Google dfw06s39-in-f17.1e100.net

As you can see, this canonical host name wouldn't mean much to a human observer.

Other canonical host names

Here is a list of some canonical host names that can be obtained using code similar to that shown in Figure 5 . Try pasting them into your browser's address window and pressing the Enter key to see which ones access the sites that youexpect and which ones don't .

Reverse lookup

At one point in the history of Java, it was possible to call the getByName method passing the IP address as a string to do a reverse lookup on an IP address. The method would return the domain name to which the IP addresswas assigned. However, this changed around Java version 1.4 and some additional code is now required to do a reverse lookup.

Do a reverse lookup on Google

Listing 6 does a reverse lookup on Google by passing one of the IP addresses to the getByName method and then calling the following methods on the InetAddress object that is returned:

  • getHostName
  • getCanonicalHostName
Listing 6 - Do a reverse lookup on Google.
System.out.println( "Display Google name using reverse lookup.");String googleAddress = address.getHostAddress(); System.out.println(InetAddress.getByName(googleAddress).getHostName()); System.out.println(InetAddress.getByName(googleAddress).getCanonicalHostName());

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask