The getAllByName method
The InetAddress class provides a static method named getAllByName that takes the domain name of a host as an incoming parameter and returns an arraycontaining references to one or more objects of type InetAddress . Each object contains an IP address and some other information related to thedomain name. The set of InetAddress objects in the array contain all of the IP addresses that are currently assigned to that domain name.
Get all for Google
The code in Listing 1 calls the getAllByName method passing the domain name for Google as a parameter. It receives a reference to an array containingreferences to one or more InetAddress objects. The set of InetAddress objects encapsulate a list of five IP addresses that are currently assigned to Google.
Display the InetAddress objects
Listing 2 contains a for loop that displays the toString version of the information encapsulated in each of the InetAddress objects.
Listing 2 - Display the Google InetAddress objects. |
---|
for(int cnt=0; cnt<addresses.length;cnt++){
System.out.println(addresses[cnt]);
}//end for loop |
Figure 2 shows the output produced by Listing 1 and Listing 2 .
Figure 2 - The list of Google IP addresses. |
---|
Get and display InetAddress(es) of Google URL
www.google.com/173.194.115.83www.google.com/173.194.115.84
www.google.com/173.194.115.80www.google.com/173.194.115.81
www.google.com/173.194.115.82 |
Everything to the left of the slash in the last five lines of Figure 2 shows the domain name. Everything to the right of the slash shows the IPaddresses. Note that there are duplicate domain names but there are no duplicate IP addresses.
Why only five IP addresses?
I was surprised that there are only five IP addresses in the list. Surely Google needs more than five front-end computers to handle the thousandsof incoming requests that it receives every second of every day.
Upon further investigation I noticed that if I run the same program over and over, I am likely to get different sets of five IP addresses on different runs.This suggests that in some fashion, the getAllByName method limits the number of InetAddress objects to only five of the potentially hundreds of IP addresses that are assigned to a particular host.However, this is not mentioned in the Oracle documentation. The documentation states:
"Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system."
There is clearly more going on here than I understand.
The localhost
The computer that you are using to read this module online also has an IP address and a name. The IP address, the name, and perhaps some other things as well aregrouped together under something commonly referred to as localhost . In other words, the IP address of your localhost is the IP address of thecomputer that you are using to read this module.
Get InetAddress object for localhost
The code in Listing 3 calls the static getLocalHost method of the InetAddress class to get a reference to an InetAddress object representingthe computer that I was using when I wrote this module.