InetAddress

public class InetAddress extends Object
implements Serializable
Known Direct Subclasses

An Internet Protocol (IP) address. This can be either an IPv4 address or an IPv6 address, and in practice you'll have an instance of either Inet4Address or Inet6Address (this class cannot be instantiated directly). Most code does not need to distinguish between the two families, and should use InetAddress.

An InetAddress may have a hostname (accessible via getHostName), but may not, depending on how the InetAddress was created.

IPv4 numeric address formats

The getAllByName method accepts IPv4 addresses in the "decimal-dotted-quad" form only:

  • "1.2.3.4" - 1.2.3.4

IPv6 numeric address formats

The getAllByName method accepts IPv6 addresses in the following forms (this text comes from RFC 2373, which you should consult for full details of IPv6 addressing):

  • The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the hexadecimal values of the eight 16-bit pieces of the address. Note that it is not necessary to write the leading zeros in an individual field, but there must be at least one numeral in every field (except for the case described in the next bullet). Examples:

         FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
         1080:0:0:0:8:800:200C:417A
  • Due to some methods of allocating certain styles of IPv6 addresses, it will be common for addresses to contain long strings of zero bits. In order to make writing addresses containing zero bits easier a special syntax is available to compress the zeros. The use of "::" indicates multiple groups of 16-bits of zeros. The "::" can only appear once in an address. The "::" can also be used to compress the leading and/or trailing zeros in an address. For example the following addresses:
         1080:0:0:0:8:800:200C:417A  a unicast address
         FF01:0:0:0:0:0:0:101        a multicast address
         0:0:0:0:0:0:0:1             the loopback address
         0:0:0:0:0:0:0:0             the unspecified addresses
    may be represented as:
         1080::8:800:200C:417A       a unicast address
         FF01::101                   a multicast address
         ::1                         the loopback address
         ::                          the unspecified addresses
  • An alternative form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values of the six high-order 16-bit pieces of the address, and the 'd's are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation). Examples:

         0:0:0:0:0:0:13.1.68.3
         0:0:0:0:0:FFFF:129.144.52.38
    or in compressed form:
         ::13.1.68.3
         ::FFFF:129.144.52.38

Scopes are given using a trailing % followed by the scope id, as in 1080::8:800:200C:417A%2 or 1080::8:800:200C:417A%en0. See RFC 4007 for more on IPv6's scoped address architecture.

Additionally, for backwards compatibility, IPv6 addresses may be surrounded by square brackets.

DNS caching

In Android 4.0 (Ice Cream Sandwich) and earlier, DNS caching was performed both by InetAddress and by the C library, which meant that DNS TTLs could not be honored correctly. In later releases, caching is done solely by the C library and DNS TTLs are honored.

Public Method Summary

boolean
equals(Object obj)
Compares this InetAddress instance against the specified address in obj.
byte[]
getAddress()
Returns the IP address represented by this InetAddress instance as a byte array.
static InetAddress[]
getAllByName(String host)
Gets all IP addresses associated with the given host identified by name or literal IP address.
static InetAddress
getByAddress(byte[] ipAddress)
Equivalent to getByAddress(null, ipAddress).
static InetAddress
getByAddress(String hostName, byte[] ipAddress, int scopeId)
static InetAddress
getByAddress(String hostName, byte[] ipAddress)
Returns an InetAddress corresponding to the given network-order bytes ipAddress and scopeId.
static InetAddress
getByName(String host)
Returns the address of a host according to the given host string name host.
String
getCanonicalHostName()
Returns the fully qualified hostname corresponding to this IP address.
int
String
getHostAddress()
Returns the numeric representation of this IP address (such as "127.0.0.1").
String
getHostName()
Returns the host name corresponding to this IP address.
static InetAddress
getLocalHost()
Returns an InetAddress for the local host if possible, or the loopback address otherwise.
static InetAddress
getLoopbackAddress()
Returns the IPv6 loopback address ::1 or the IPv4 loopback address 127.0.0.1.
int
hashCode()
Gets the hashcode of the represented IP address.
boolean
isAnyLocalAddress()
Returns whether this is the IPv6 unspecified wildcard address :: or the IPv4 "any" address, 0.0.0.0.
boolean
isLinkLocalAddress()
Returns whether this address is a link-local address or not.
boolean
isLoopbackAddress()
Returns whether this address is a loopback address or not.
boolean
isMCGlobal()
Returns whether this address is a global multicast address or not.
boolean
isMCLinkLocal()
Returns whether this address is a link-local multicast address or not.
boolean
isMCNodeLocal()
Returns whether this address is a node-local multicast address or not.
boolean
isMCOrgLocal()
Returns whether this address is a organization-local multicast address or not.
boolean
isMCSiteLocal()
Returns whether this address is a site-local multicast address or not.
boolean
isMulticastAddress()
Returns whether this address is a multicast address or not.
boolean
isReachable(int timeout)
Tries to reach this InetAddress.
boolean
isReachable(NetworkInterface networkInterface, int ttl, int timeout)
Tries to reach this InetAddress.
boolean
isSiteLocalAddress()
Returns whether this address is a site-local address or not.
String
toString()
Returns a string containing the host name (if available) and host address.

Inherited Method Summary