java.net

Class InetAddress

public class InetAddress extends Object implements Serializable

This class models an Internet address. It does not have a public constructor. Instead, new instances of this objects are created using the static methods getLocalHost(), getByName(), and getAllByName().

This class fulfills the function of the C style functions gethostname(), gethostbyname(), and gethostbyaddr(). It resolves Internet DNS names into their corresponding numeric addresses and vice versa.

UNKNOWN: This class is not final since JDK 1.4

Method Summary
booleanequals(Object obj)
Tests this address for equality against another InetAddress.
byte[]getAddress()
Returns the IP address of this object as a byte array.
static InetAddress[]getAllByName(String hostname)
Returns an array of InetAddress objects representing all the host/ip addresses of a given host, given the host's name.
static InetAddressgetByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address.
static InetAddressgetByAddress(String host, byte[] addr)
Creates an InetAddress based on the provided host name and IP address.
static InetAddressgetByName(String hostname)
Returns an InetAddress object representing the IP address of the given hostname.
StringgetCanonicalHostName()
Returns the canonical hostname represented by this InetAddress
StringgetHostAddress()
Returns the IP address of this object as a String.
StringgetHostName()
Returns the hostname for this address.
static InetAddressgetLocalHost()
Returns an InetAddress object representing the address of the current host.
inthashCode()
Returns a hash value for this address.
booleanisAnyLocalAddress()
Utility routine to check if the InetAddress in a wildcard address

This method cannot be abstract for backward compatibility reasons.

booleanisLinkLocalAddress()
Utility routine to check if InetAddress is a link local address

This method cannot be abstract for backward compatibility reasons.

booleanisLoopbackAddress()
Utility routine to check if the InetAddress is a loopback address

This method cannot be abstract for backward compatibility reasons.

booleanisMCGlobal()
Utility routine to check if InetAddress is a global multicast address

This method cannot be abstract for backward compatibility reasons.

booleanisMCLinkLocal()
Utility routine to check if InetAddress is a link local multicast address.
booleanisMCNodeLocal()
Utility routine to check if InetAddress is a node local multicast address.
booleanisMCOrgLocal()
Utility routine to check if InetAddress is a organization local multicast address.
booleanisMCSiteLocal()
Utility routine to check if InetAddress is a site local multicast address.
booleanisMulticastAddress()
Returns true if this address is a multicast address, false otherwise.
booleanisSiteLocalAddress()
Utility routine to check if InetAddress is a site local address

This method cannot be abstract for backward compatibility reasons.

StringtoString()
Converts this address to a String.

Method Detail

equals

public boolean equals(Object obj)
Tests this address for equality against another InetAddress. The two addresses are considered equal if they contain the exact same octets. This implementation overrides Object.equals()

Parameters: obj The address to test for equality

Returns: true if the passed in object's address is equal to this one's, false otherwise

getAddress

public byte[] getAddress()
Returns the IP address of this object as a byte array.

Returns: IP address

getAllByName

public static InetAddress[] getAllByName(String hostname)
Returns an array of InetAddress objects representing all the host/ip addresses of a given host, given the host's name. This name can be either a hostname such as "www.urbanophile.com" or an IP address in dotted decimal format such as "127.0.0.1". If the value is null, the hostname of the local machine is supplied by default.

Parameters: hostname The name of the desired host, or null for the local loopback address.

Returns: All addresses of the host as an array of InetAddress objects.

Throws: UnknownHostException If no IP address for the host could be found SecurityException If a security manager exists and its checkConnect method doesn't allow the operation

getByAddress

public static InetAddress getByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address. The argument is in network byte order: the highest order byte of the address is in getAddress()[0].

Parameters: addr The IP address to create the InetAddress object from

Throws: UnknownHostException If IP address has illegal length

Since: 1.4

getByAddress

public static InetAddress getByAddress(String host, byte[] addr)
Creates an InetAddress based on the provided host name and IP address. No name service is checked for the validity of the address.

Parameters: host The hostname of the InetAddress object to create addr The IP address to create the InetAddress object from

Throws: UnknownHostException If IP address is of illegal length

Since: 1.4

getByName

public static InetAddress getByName(String hostname)
Returns an InetAddress object representing the IP address of the given hostname. This name can be either a hostname such as "www.urbanophile.com" or an IP address in dotted decimal format such as "127.0.0.1". If the hostname is null or "", the hostname of the local machine is supplied by default. This method is equivalent to returning the first element in the InetAddress array returned from GetAllByName.

Parameters: hostname The name of the desired host, or null for the local loopback address.

Returns: The address of the host as an InetAddress object.

Throws: UnknownHostException If no IP address for the host could be found SecurityException If a security manager exists and its checkConnect method doesn't allow the operation

getCanonicalHostName

public String getCanonicalHostName()
Returns the canonical hostname represented by this InetAddress

Since: 1.4

getHostAddress

public String getHostAddress()
Returns the IP address of this object as a String.

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Returns: The IP address of this object in String form

Since: 1.0.2

getHostName

public String getHostName()
Returns the hostname for this address. This will return the IP address as a String if there is no hostname available for this address

Returns: The hostname for this address

getLocalHost

public static InetAddress getLocalHost()
Returns an InetAddress object representing the address of the current host.

Returns: The local host's address

Throws: UnknownHostException If no IP address for the host could be found

hashCode

public int hashCode()
Returns a hash value for this address. Useful for creating hash tables. Overrides Object.hashCode()

Returns: A hash value for this address.

isAnyLocalAddress

public boolean isAnyLocalAddress()
Utility routine to check if the InetAddress in a wildcard address

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isLinkLocalAddress

public boolean isLinkLocalAddress()
Utility routine to check if InetAddress is a link local address

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isLoopbackAddress

public boolean isLoopbackAddress()
Utility routine to check if the InetAddress is a loopback address

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isMCGlobal

public boolean isMCGlobal()
Utility routine to check if InetAddress is a global multicast address

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isMCLinkLocal

public boolean isMCLinkLocal()
Utility routine to check if InetAddress is a link local multicast address.

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isMCNodeLocal

public boolean isMCNodeLocal()
Utility routine to check if InetAddress is a node local multicast address.

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isMCOrgLocal

public boolean isMCOrgLocal()
Utility routine to check if InetAddress is a organization local multicast address.

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isMCSiteLocal

public boolean isMCSiteLocal()
Utility routine to check if InetAddress is a site local multicast address.

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

isMulticastAddress

public boolean isMulticastAddress()
Returns true if this address is a multicast address, false otherwise. An address is multicast if the high four bits are "1110". These are also known as "Class D" addresses.

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Returns: true if mulitcast, false if not

Since: 1.1

isSiteLocalAddress

public boolean isSiteLocalAddress()
Utility routine to check if InetAddress is a site local address

This method cannot be abstract for backward compatibility reasons. By default it always throws {@link UnsupportedOperationException} unless overridden.

Since: 1.4

toString

public String toString()
Converts this address to a String. This string contains the IP in dotted decimal form. For example: "127.0.0.1" This method is equivalent to getHostAddress() and overrides Object.toString()

Returns: This address in String form