JavaTM 2 Platform
Std. Ed. v1.3.1

java.net
Class InetAddress

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

public final class InetAddress
extends Object
implements Serializable

This class represents an Internet Protocol (IP) 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 equals(Object obj)
          Compares this object against the specified object.
 byte[] getAddress()
          Returns the raw IP address of this InetAddress object.
static InetAddress[] getAllByName(String host)
          Determines all the IP addresses of a host, given the host's name.
static InetAddress getByName(String host)
          Determines the IP address of a host, given the host's name.
 String getHostAddress()
          Returns the IP address string "%d.%d.%d.%d".
 String getHostName()
          Gets the host name for this IP address.
static InetAddress getLocalHost()
          Returns the local host.
 int hashCode()
          Returns a hashcode for this IP address.
 boolean isMulticastAddress()
          Utility routine to check if the InetAddress is an IP multicast address.
 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 an IP multicast address. IP multicast address is a Class D address i.e first four bits of the address are 1110.
Returns:
a boolean indicating if the InetAddress is an IP multicast address
Since:
JDK1.1

getHostName

public String getHostName()
Gets the host name for this IP address.

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:
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation .
See Also:
SecurityManager.checkConnect(java.lang.String, int)

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 String getHostAddress()
Returns the IP address string "%d.%d.%d.%d".
Returns:
the raw IP address in a string format.
Since:
JDK1.0.2

hashCode

public int hashCode()
Returns a hashcode for this IP address.
Overrides:
hashCode in class Object
Returns:
a hash code value for this IP address.

equals

public boolean equals(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 Object
Parameters:
obj - the object to compare against.
Returns:
true if the objects are the same; false otherwise.
See Also:
getAddress()

toString

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

getByName

public static InetAddress getByName(String host)
                             throws 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 "206.26.48.100".
Parameters:
host - the specified host, or null for the local host.
Returns:
an IP address for the given host name.
Throws:
UnknownHostException - if no IP address for the host could be found.

getAllByName

public static InetAddress[] getAllByName(String host)
                                  throws 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 "206.26.48.100".

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:
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.
See Also:
SecurityManager.checkConnect(java.lang.String, int)

getLocalHost

public static InetAddress getLocalHost()
                                throws 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:
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.
See Also:
SecurityManager.checkConnect(java.lang.String, int)

JavaTM 2 Platform
Std. Ed. v1.3.1

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Java, Java 2D, and JDBC are trademarks or registered trademarks of Oracle and/or its affiliates, in the US and other countries.
Copyright © 1995, 2010 Oracle and/or its affiliates. All rights reserved.