java.net
Class InetAddress

java.lang.Object
  |
  +--java.net.InetAddress
All Implemented Interfaces:
java.io.Serializable

public final class InetAddress
extends java.lang.Object
implements java.io.Serializable

This class represents an Internet Protocol version 6 (IPv6) address.

Applications should use the methods getLocalHost, getByName, or getAllByName to create a new InetAddress instance.

Since:
JDK1.0
See Also:
getAllByName(java.lang.String), getByName(java.lang.String), getLocalHost(), Serialized Form

Method Summary
 boolean compare(byte[] tab1, byte[] tab2)
           
 boolean equals(java.lang.Object obj)
          Compares this object against the specified object.
 byte[] getAddress()
          Returns the raw IP address of this InetAddress object.
static InetAddress[] getAllByName(java.lang.String host)
          Determines all the IP addresses of a host, given the host's name.
static InetAddress getByName(java.lang.String host)
          Determines the IP address of a host, given the host's name.
 java.lang.String getHostAddress()
          Returns the IP address string.
 java.lang.String getHostName()
          Returns the hostname for this address.
static InetAddress getLocalHost()
          Returns the local host.
 int hashCode()
          Returns a hashcode for this IP address.
 boolean isMappedAddress()
          Utility routine to check if the InetAddress is an IPv4-mapped one.
 boolean isMulticastAddress()
          Utility routine to check if the InetAddress is a IP multicast address (pure IPv6 or IPv4-mapped).
 java.lang.String toString()
          Converts this IP address to a String.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

isMulticastAddress

public boolean isMulticastAddress()
Utility routine to check if the InetAddress is a IP multicast address (pure IPv6 or IPv4-mapped).
Since:
JDK1.1

isMappedAddress

public boolean isMappedAddress()
Utility routine to check if the InetAddress is an IPv4-mapped one.
Since:
JDK1.1

getHostName

public java.lang.String getHostName()
Returns the hostname for this address. If the host is equal to null, then this address refers to any of the local machine's available network addresses.

If there is a security manager, its checkConnect method is first called with the hostname and -1 as its arguments to see if the operation is allowed.

Returns:
the host name for this IP address.
Throws:
java.lang.SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation .
See Also:
SecurityManager.checkConnect(java.lang.String, int)

compare

public boolean compare(byte[] tab1,
                       byte[] tab2)

getAddress

public byte[] getAddress()
Returns the raw IP address of this InetAddress object. The result is in network byte order: the highest order byte of the address is in getAddress()[0].
Returns:
the raw IP address of this object.

getHostAddress

public java.lang.String getHostAddress()
Returns the IP address string.
Returns:
the raw IP address in a string format.
Since:
JDK1.0.2

hashCode

public int hashCode()
Returns a hashcode for this IP address. The hash function just converts all bytes of the 128 bits address into int and sums them.
Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this IP address.

equals

public boolean equals(java.lang.Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and it represents the same IP address as this object.

Two instances of InetAddress represent the same IP address if the length of the byte arrays returned by getAddress is the same for both, and each of the array components is the same for the byte arrays.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to compare against.
Returns:
true if the objects are the same; false otherwise.
See Also:
getAddress()

toString

public java.lang.String toString()
Converts this IP address to a String.
Overrides:
toString in class java.lang.Object
Returns:
a string representation of this IP address.

getByName

public static InetAddress getByName(java.lang.String host)
                             throws java.net.UnknownHostException
Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a string representing its IP address, such as "16ea::14d2:1345".
Parameters:
host - the specified host, or null for the local host.
Returns:
an IP address for the given host name.
Throws:
java.net.UnknownHostException - if no IP address for the host could be found.

getAllByName

public static InetAddress[] getAllByName(java.lang.String host)
                                  throws java.net.UnknownHostException
Determines all the IP addresses of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a string representing its IP address, such as "16ea::14d2:1345".

If there is a security manager and host is not null and host.length() is not equal to zero, the security manager's checkConnect method is called with the hostname and -1 as its arguments to see if the operation is allowed.

Parameters:
host - the name of the host.
Returns:
an array of all the IP addresses for a given host name.
Throws:
java.net.UnknownHostException - if no IP address for the host could be found.
java.lang.SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
SecurityManager.checkConnect(java.lang.String, int)

getLocalHost

public static InetAddress getLocalHost()
                                throws java.net.UnknownHostException
Returns the local host.

If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed.

Returns:
the IP address of the local host.
Throws:
java.net.UnknownHostException - if no IP address for the host could be found.
java.lang.SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
SecurityManager.checkConnect(java.lang.String, int)