Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.net
Class InetAddress  view InetAddress download InetAddress.java

java.lang.Object
  extended byjava.net.InetAddress
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
Inet4Address, Inet6Address

public class InetAddress
extends java.lang.Object
implements java.io.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.


Field Summary
(package private)  byte[] addr
          An array of octets representing an IP address.
private  int address
          The Serialized Form specifies that an int 'address' is saved/restored.
(package private) static InetAddress ANY_IF
          Dummy InetAddress, used to bind socket to any (all) network interfaces.
(package private)  int family
          The field 'family' seems to be the AF_ value.
(package private)  java.lang.String hostName
          The name of the host for this address.
private static InetAddress inaddr_any
          The special IP address INADDR_ANY.
(package private) static InetAddress LOCALHOST
          Stores static localhost address object.
private static long serialVersionUID
           
 
Constructor Summary
(package private) InetAddress(byte[] ipaddr, java.lang.String hostname)
          Initializes this object's addr instance variable from the passed in byte array.
 
Method Summary
private static byte[] aton(java.lang.String hostname)
          If hostname is a valid numeric IP address, return the numeric address.
 boolean equals(java.lang.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(java.lang.String hostname)
          Returns an array of InetAddress objects representing all the host/ip addresses of a given host, given the host's name.
static InetAddress getByAddress(byte[] addr)
          Returns an InetAddress object given the raw IP address.
static InetAddress getByAddress(java.lang.String host, byte[] addr)
          Creates an InetAddress based on the provided host name and IP address.
static InetAddress getByName(java.lang.String hostname)
          Returns an InetAddress object representing the IP address of the given hostname.
 java.lang.String getCanonicalHostName()
          Returns the canonical hostname represented by this InetAddress
 java.lang.String getHostAddress()
          Returns the IP address of this object as a String.
 java.lang.String getHostName()
          Returns the hostname for this address.
(package private) static InetAddress getInaddrAny()
          Returns the special address INADDR_ANY used for binding to a local port on all IP addresses hosted by a the local host.
static InetAddress getLocalHost()
          Returns an InetAddress object representing the address of the current host.
 int hashCode()
          Returns a hash value for this address.
 boolean isAnyLocalAddress()
          Utility routine to check if the InetAddress in a wildcard address
 boolean isLinkLocalAddress()
          Utility routine to check if InetAddress is a link local address
 boolean isLoopbackAddress()
          Utility routine to check if the InetAddress is a loopback address
 boolean isMCGlobal()
          Utility routine to check if InetAddress is a global multicast address
 boolean isMCLinkLocal()
          Utility routine to check if InetAddress is a link local multicast address.
 boolean isMCNodeLocal()
          Utility routine to check if InetAddress is a node local multicast address.
 boolean isMCOrgLocal()
          Utility routine to check if InetAddress is a organization local multicast address.
 boolean isMCSiteLocal()
          Utility routine to check if InetAddress is a site local multicast address.
 boolean isMulticastAddress()
          Returns true if this address is a multicast address, false otherwise.
 boolean isSiteLocalAddress()
          Utility routine to check if InetAddress is a site local address
private  void readObject(java.io.ObjectInputStream ois)
           
private  void readResolve()
           
 java.lang.String toString()
          Converts this address to a String.
private  void writeObject(java.io.ObjectOutputStream oos)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

inaddr_any

private static InetAddress inaddr_any
The special IP address INADDR_ANY.


ANY_IF

static InetAddress ANY_IF
Dummy InetAddress, used to bind socket to any (all) network interfaces.


LOCALHOST

static InetAddress LOCALHOST
Stores static localhost address object.


address

private int address
The Serialized Form specifies that an int 'address' is saved/restored. This class uses a byte array internally so we'll just do the conversion at serialization time and leave the rest of the algorithm as is.


addr

transient byte[] addr
An array of octets representing an IP address.


hostName

java.lang.String hostName
The name of the host for this address.


family

int family
The field 'family' seems to be the AF_ value. FIXME: Much of the code in the other java.net classes does not make use of this family field. A better implementation would be to make use of getaddrinfo() and have other methods just check the family field rather than examining the length of the address each time.

Constructor Detail

InetAddress

InetAddress(byte[] ipaddr,
            java.lang.String hostname)
Initializes this object's addr instance variable from the passed in byte array. Note that this constructor is protected and is called only by static methods in this class.

Method Detail

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.

Since:
1.1

isAnyLocalAddress

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

Since:
1.4

isLoopbackAddress

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

Since:
1.4

isLinkLocalAddress

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

Since:
1.4

isSiteLocalAddress

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

Since:
1.4

isMCGlobal

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

Since:
1.4

isMCNodeLocal

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

Since:
1.4

isMCLinkLocal

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

Since:
1.4

isMCSiteLocal

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

Since:
1.4

isMCOrgLocal

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

Since:
1.4

getHostName

public java.lang.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


getCanonicalHostName

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

Since:
1.4

getAddress

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


getHostAddress

public java.lang.String getHostAddress()
Returns the IP address of this object as a String. The address is in the dotted octet notation, for example, "127.0.0.1".

Since:
1.0.2

hashCode

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


equals

public boolean equals(java.lang.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()


toString

public java.lang.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()


getByAddress

public static InetAddress getByAddress(byte[] addr)
                                throws UnknownHostException
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].

Since:
1.4

getByAddress

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

Since:
1.4

aton

private static byte[] aton(java.lang.String hostname)
If hostname is a valid numeric IP address, return the numeric address. Otherwise, return null.


getByName

public static InetAddress getByName(java.lang.String hostname)
                             throws UnknownHostException
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.


getAllByName

public static InetAddress[] getAllByName(java.lang.String hostname)
                                  throws UnknownHostException
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.


getInaddrAny

static InetAddress getInaddrAny()
                         throws UnknownHostException
Returns the special address INADDR_ANY used for binding to a local port on all IP addresses hosted by a the local host.


getLocalHost

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


readResolve

private void readResolve()
                  throws java.io.ObjectStreamException

readObject

private void readObject(java.io.ObjectInputStream ois)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException

writeObject

private void writeObject(java.io.ObjectOutputStream oos)
                  throws java.io.IOException