<< Chapter < Page Chapter >> Page >

Methods of the InetAddress class

The InetAddress class provides several static methods that return a reference to an object of type InetAddress . You can use those methods to deal with and to manipulate IP addresses and domain names.

For example, the static getByName(String host) method returns a reference to an InetAddress object representing the host whose domain name is passed as a parameter to the method. The resulting object can be used todetermine the IP address and the canonical host name of the host.

There is a problem with this, however. As I will explain later, many hosts have multiple IP addresses. To accommodate this, the InetAddress class provides a method named getAllByName(String host) that can be used to get an array of references to InetAddress objects representing IP addresses assigned to the host.

The getLocalHost method returns a reference to an InetAddress object representing the local host computer.

There are also a variety of methods that can be called on an InetAddress object to get information about the host that is represented by that object.

Discussion and sample code

I will present and explain a program named Java4630a that illustrates various aspects of the InetAddress class in this module.

I will explain the program in fragments. A complete listing of the program is provided in Listing 8 .

The program output

The program produces the screen output shown in Figure 1 when run on my computer. However, the information regarding the LocalHost will be different when you run this program on your computer.

Figure 1 - Program output.
Get and display InetAddress(es) of Google URL www.google.com/173.194.115.17www.google.com/173.194.115.18 www.google.com/173.194.115.19www.google.com/173.194.115.20 www.google.com/173.194.115.16Get and display current InetAddress of LocalHost dell8700/192.168.2.16Extract and display current name of LocalHost dell8700Extract and display current address of LocalHost 192.168.2.16Display canonical host name for Google dfw06s39-in-f17.1e100.netDisplay Google name using reverse lookup. dfw06s39-in-f17.1e100.netdfw06s39-in-f17.1e100.net

I will refer to this output in context as I explain the various elements of the program.

Beginning of the program

This is a very simple program consisting solely of the main method in a class named Java4630a . The program begins in Listing 1 .

Listing 1 - Beginning of the program.
import java.net.*; class Java4630a{public static void main(String[] args){try{ System.out.println("Get and display InetAddress(es) of Google URL"); InetAddress[]addresses = InetAddress.getAllByName("www.google.com");

Domain names and IP addresses

There is not necessarily a one-to-one correspondence between IP addresses and domain names. In fact there can be a many-to-one correspondence betweenthe two.

Every computer on the Internet must have a unique IP address, but multiple computers can have (or can respond to) the same domain name. For example, if the domain name, www.google.com , were required to apply to a single computer, that computer would require an enormous amount of bandwidth andprocessing power to accommodate all of the search requests that are made to that domain name everysecond of every day.

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