DthingApi
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
java.net.InetAddress Class Reference
Inheritance diagram for java.net.InetAddress:
java.io.Serializable java.net.Inet4Address

Public Member Functions

boolean equals (Object obj)
 
byte [] getAddress ()
 
int getAddressToInt ()
 
int getFamily ()
 
String getHostAddress ()
 
int hashCode ()
 
String toString ()
 
boolean isAnyLocalAddress ()
 
boolean isLinkLocalAddress ()
 
boolean isLoopbackAddress ()
 
boolean isMCGlobal ()
 
boolean isMCLinkLocal ()
 
boolean isMCNodeLocal ()
 
boolean isMCOrgLocal ()
 
boolean isMCSiteLocal ()
 
boolean isMulticastAddress ()
 
boolean isSiteLocalAddress ()
 

Static Public Member Functions

static InetAddress getLocalHost () throws UnknownHostException
 
static void clearDnsCache ()
 
static InetAddress getByAddress (byte[] addr) throws UnknownHostException
 
static InetAddress getByName (String host) throws UnknownHostException
 
static InetAddress getByAddress (String hostName, byte[] ipAddress) throws UnknownHostException
 

Static Public Attributes

static final InetAddress UNSPECIFIED
 

Protected Attributes

byte [] ipAddress
 
String hostName
 

Detailed Description

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:

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):

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

On Android, addresses are cached for 600 seconds (10 minutes) by default. Failed lookups are cached for 10 seconds. The underlying C library or OS may cache for longer, but you can control the Java-level caching with the usual

"networkaddress.cache.ttl"

and

"networkaddress.cache.negative.ttl"

system properties. These are parsed as integer numbers of seconds, where the special value 0 means "don't cache" and -1 means "cache forever".

Note also that on Android – unlike the RI – the cache is not unbounded. The current implementation caches around 512 entries, removed on a least-recently-used basis. (Obviously, you should not rely on these details.)

See also
Inet4Address
Inet6Address

Member Function Documentation

◆ clearDnsCache()

static void java.net.InetAddress.clearDnsCache ( )
inlinestatic

Removes all entries from the VM's DNS cache. This does not affect the C library's DNS cache, nor any caching DNS servers between you and the canonical server.

◆ equals()

boolean java.net.InetAddress.equals ( Object  obj)
inline

Compares this

InetAddress

instance against the specified address in

obj

. Two addresses are equal if their address byte arrays have the same length and if the bytes in the arrays are equal.

Parameters
objthe object to be tested for equality.
Returns
true
if both objects are equal,
false
otherwise.

◆ getAddress()

byte [] java.net.InetAddress.getAddress ( )
inline

Returns the IP address represented by this

InetAddress

instance as a byte array. The elements are in network order (the highest order address byte is in the zeroth element).

Returns
the address in form of a byte array.

◆ getByAddress() [1/2]

static InetAddress java.net.InetAddress.getByAddress ( byte []  addr) throws UnknownHostException
inlinestatic

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

.

This method doesn't block, i.e. no reverse name service lookup is performed.

IPv4 address byte array must be 4 bytes long and IPv6 byte array must be 16 bytes long

Parameters
addrthe raw IP address in network byte order
Returns
an InetAddress object created from the raw IP address.
Exceptions
UnknownHostExceptionif IP address is of illegal length

◆ getByAddress() [2/2]

static InetAddress java.net.InetAddress.getByAddress ( String  hostName,
byte []  ipAddress 
) throws UnknownHostException
inlinestatic

Returns an

InetAddress

corresponding to the given network-order bytes

ipAddress

and

scopeId

.

For an IPv4 address, the byte array must be of length 4. For IPv6, the byte array must be of length 16. Any other length will cause an

UnknownHostException

.

No reverse lookup is performed. The given

hostName

(which may be null) is associated with the new

InetAddress

with no validation done.

(Note that numeric addresses such as

"127.0.0.1"

are names for the purposes of this API. Most callers probably want getAllByName instead.)

Exceptions
UnknownHostExceptionif
ipAddress
is null or the wrong length.

◆ getByName()

static InetAddress java.net.InetAddress.getByName ( String  host) throws UnknownHostException
inlinestatic

Determines the IP address of a host, given the host's name.

The host name can either be a machine name, such as "@code java.sun.com @endcode ", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

For

host

specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted. IPv6 scoped addresses are also supported. See here for a description of IPv6 scoped addresses.

If the host is

null

then an

InetAddress

representing a IPv4 address of the loopback interface is returned. See RFC 3330 section 2 and RFC 2373 section 2.5.3.

Parameters
hostthe specified host, or
null
.
Returns
an IP address for the given host name.
Exceptions
UnknownHostExceptionif no IP address for the
host
could be found, or if a scope_id was specified for a global IPv6 address.

◆ getLocalHost()

static InetAddress java.net.InetAddress.getLocalHost ( ) throws UnknownHostException
inlinestatic

Returns an

InetAddress

for the local host if possible, or the loopback address otherwise. This method works by getting the hostname, performing a DNS lookup, and then taking the first returned address. For devices with multiple network interfaces and/or multiple addresses per interface, this does not necessarily return the

InetAddress

you want.

Multiple interface/address configurations were relatively rare when this API was designed, but multiple interfaces are the default for modern mobile devices (with separate wifi and radio interfaces), and the need to support both IPv4 and IPv6 has made multiple addresses commonplace. New code should thus avoid this method except where it's basically being used to get a loopback address or equivalent.

There are two main ways to get a more specific answer:

  • If you have a connected socket, you should probably use Socket#getLocalAddress instead: that will give you the address that's actually in use for that connection. (It's not possible to ask the question "what local address would a connection to a given remote address use?"; you have to actually make the connection and see.)
  • For other use cases, see NetworkInterface, which lets you enumerate all available network interfaces and their addresses.

Note that if the host doesn't have a hostname set – as Android devices typically don't – this method will effectively return the loopback address, albeit by getting the name

localhost

and then doing a lookup to translate that to

127.0.0.1

.

Returns
an
InetAddress
representing the local host, or the loopback address.
Exceptions
UnknownHostExceptionif the address lookup fails.

◆ hashCode()

int java.net.InetAddress.hashCode ( )
inline

Gets the hashcode of the represented IP address.

Returns
the appropriate hashcode value.

◆ isAnyLocalAddress()

boolean java.net.InetAddress.isAnyLocalAddress ( )
inline

Returns whether this is the IPv6 unspecified wildcard address

::

or the IPv4 "any" address,

0.0.0.0

.

◆ isLinkLocalAddress()

boolean java.net.InetAddress.isLinkLocalAddress ( )
inline

Returns whether this address is a link-local address or not.

Valid IPv6 link-local addresses have the prefix

fe80::/10

.

RFC 3484 "Default Address Selection for Internet Protocol Version 6 (IPv6)" states that both IPv4 auto-configuration addresses (prefix

169.254/16

) and IPv4 loopback addresses (prefix

127/8

) have link-local scope, but Inet4Address only considers the auto-configuration addresses to have link-local scope. That is: the IPv4 loopback address returns false.

◆ isLoopbackAddress()

boolean java.net.InetAddress.isLoopbackAddress ( )
inline

Returns whether this address is a loopback address or not.

Valid IPv4 loopback addresses have the prefix

127/8

.

The only valid IPv6 loopback address is

::1

.

◆ isMCGlobal()

boolean java.net.InetAddress.isMCGlobal ( )
inline

Returns whether this address is a global multicast address or not.

Valid IPv6 global multicast addresses have the prefix

ffxe::/16

, where

x

is a set of flags and the additional 112 bits make up the global multicast address space.

Valid IPv4 global multicast addresses are the range of addresses from

224.0.1.0

to

238.255.255.255

.

◆ isMCLinkLocal()

boolean java.net.InetAddress.isMCLinkLocal ( )
inline

Returns whether this address is a link-local multicast address or not.

Valid IPv6 link-local multicast addresses have the prefix

ffx2::/16

, where x is a set of flags and the additional 112 bits make up the link-local multicast address space.

Valid IPv4 link-local multicast addresses have the prefix

224.0.0/24

.

◆ isMCNodeLocal()

boolean java.net.InetAddress.isMCNodeLocal ( )
inline

Returns whether this address is a node-local multicast address or not.

Valid IPv6 node-local multicast addresses have the prefix

ffx1::/16

, where x is a set of flags and the additional 112 bits make up the link-local multicast address space.

There are no valid IPv4 node-local multicast addresses.

◆ isMCOrgLocal()

boolean java.net.InetAddress.isMCOrgLocal ( )
inline

Returns whether this address is a organization-local multicast address or not.

Valid IPv6 organization-local multicast addresses have the prefix

ffx8::/16

, where x is a set of flags and the additional 112 bits make up the link-local multicast address space.

Valid IPv4 organization-local multicast addresses have the prefix

239.192/14

.

◆ isMCSiteLocal()

boolean java.net.InetAddress.isMCSiteLocal ( )
inline

Returns whether this address is a site-local multicast address or not.

Valid IPv6 site-local multicast addresses have the prefix

ffx5::/16

, where x is a set of flags and the additional 112 bits make up the link-local multicast address space.

Valid IPv4 site-local multicast addresses have the prefix

239.255/16

.

◆ isMulticastAddress()

boolean java.net.InetAddress.isMulticastAddress ( )
inline

Returns whether this address is a multicast address or not.

Valid IPv6 multicast addresses have the prefix

ff::/8

.

Valid IPv4 multicast addresses have the prefix

224/4

.

◆ isSiteLocalAddress()

boolean java.net.InetAddress.isSiteLocalAddress ( )
inline

Returns whether this address is a site-local address or not.

For the purposes of this method, valid IPv6 site-local addresses have the deprecated prefix

fec0::/10

from RFC 1884, not the modern prefix

fc00::/7

from RFC 4193.

RFC 3484 "Default Address Selection for Internet Protocol Version 6 (IPv6)" states that IPv4 private addresses have the prefix

10/8

,

172.16/12

, or

192.168/16

.

Returns
true
if this instance represents a site-local address,
false
otherwise.

◆ toString()

String java.net.InetAddress.toString ( )
inline

Returns a string containing a concise, human-readable description of this IP address.

Returns
the description, as host/address.

Member Data Documentation

◆ UNSPECIFIED

final InetAddress java.net.InetAddress.UNSPECIFIED
static
Initial value:
= new InetAddress(NetConstants.AF_UNSPEC, null,
null)

Used by the DatagramSocket.disconnect implementation.

internal use only


The documentation for this class was generated from the following file: