img
. .
On the Internet, it is common for a single name to be used to represent several machines.
In the world of web servers, this is one way to provide some degree of scaling. The
getAllByName( ) factory method returns an array of InetAddresses that represent all of
the addresses that a particular name resolves to. It will also throw an UnknownHostException
if it can't resolve the name to at least one address.
InetAddress also includes the factory method getByAddress( ), which takes an IP
address and returns an InetAddress object. Either an IPv4 or an IPv6 address can be used.
The following example prints the addresses and names of the local machine and two
well-known Internet web sites:
// Demonstrate InetAddress.
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("osborne.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
Here is the output produced by this program. (Of course, the output you see may be
slightly different.)
default/206.148.209.138
osborne.com/198.45.24.162
www.nba.com/64.5.96.214
www.nba.com/64.5.96.216
Instance Methods
The InetAddress class has several other methods, which can be used on the objects returned
by the methods just discussed. Here are some of the more commonly used methods:
boolean equals(Object other)
Returns true if this object has the same Internet address as other.
byte[ ] getAddress( )
Returns a byte array that represents the object's IP address in
network byte order.
String getHostAddress( )
Returns a string that represents the host address associated
with the InetAddress object.
String getHostName( )
Returns a string that represents the host name associated with
the InetAddress object.
boolean isMulticastAddress( )
Returns true if this address is a multicast address. Other wise,
it returns false.
String toString( )
Returns a string that lists the host name and the IP address for
convenience.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home