Top Description Inners Fields Constructors Methods
java.net

public sealed Class InetAddress

extends Object
implements Serializable
permits Inet4Address, Inet6Address
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
java.net.Inet4Address, java.net.Inet6Address
Imports
java.net.spi.InetAddressResolver, .InetAddressResolverProvider, .InetAddressResolver.LookupPolicy, java.security.AccessController, .PrivilegedAction, java.util.List, .NavigableSet, .ArrayList, .Objects, .Scanner, .ServiceLoader, .Arrays, java.io.File, .ObjectStreamException, .ObjectStreamField, .IOException, .InvalidObjectException, .ObjectInputStream, .ObjectInputStream.GetField, .ObjectOutputStream, .ObjectOutputStream.PutField, .Serializable, java.lang.annotation.Native, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, .ConcurrentSkipListSet, java.util.concurrent.atomic.AtomicLong, java.util.concurrent.locks.Lock, .ReentrantLock, java.util.stream.Stream, jdk.internal.access.JavaNetInetAddressAccess, .SharedSecrets, jdk.internal.misc.Blocker, .VM, jdk.internal.vm.annotation.Stable, sun.net.ResolverProviderConfiguration, .InetAddressCachePolicy, sun.security.action.*, sun.net.util.IPAddressUtil, sun.nio.cs.UTF_8

This class represents an Internet Protocol (IP) address.

An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built. The IP address architecture is defined by RFC 790: Assigned Numbers, RFC 1918: Address Allocation for Private Internets, RFC 2365: Administratively Scoped IP Multicast, and RFC 2373: IP Version 6 Addressing Architecture. An instance of an InetAddress consists of an IP address and possibly its corresponding host name (depending on whether it is constructed with a host name or whether it has already done reverse host name resolution).

Address types

Description of unicast and multicast address types
Address TypeDescription
unicast An identifier for a single interface. A packet sent to a unicast address is delivered to the interface identified by that address.

The Unspecified Address -- Also called anylocal or wildcard address. It must never be assigned to any node. It indicates the absence of an address. One example of its use is as the target of bind, which allows a server to accept a client connection on any interface, in case the server host has multiple interfaces.

The unspecified address must not be used as the destination address of an IP packet.

The Loopback Addresses -- This is the address assigned to the loopback interface. Anything sent to this IP address loops around and becomes IP input on the local host. This address is often used when testing a client.

multicast An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast address is delivered to all interfaces identified by that address.

IP address scope

Link-local addresses are designed to be used for addressing on a single link for purposes such as auto-address configuration, neighbor discovery, or when no routers are present.

Site-local addresses are designed to be used for addressing inside of a site without the need for a global prefix.

Global addresses are unique across the internet.

Textual representation of IP addresses

The textual representation of an IP address is address family specific.

For IPv4 address format, please refer to Inet4Address#format; For IPv6 address format, please refer to Inet6Address#format.

There is a couple of System Properties affecting how IPv4 and IPv6 addresses are used.

Host Name Resolution

The InetAddress class provides methods to resolve host names to their IP addresses and vice versa. The actual resolution is delegated to an InetAddress resolver.

Host name-to-IP address resolution maps a host name to an IP address. For any host name, its corresponding IP address is returned.

Reverse name resolution means that for any IP address, the host associated with the IP address is returned.

The built-in InetAddress resolver implementation does host name-to-IP address resolution and vice versa through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and the Lightweight Directory Access Protocol (LDAP). The particular naming services that the built-in resolver uses by default depends on the configuration of the local machine.

InetAddress has a service provider mechanism for InetAddress resolvers that allows a custom InetAddress resolver to be used instead of the built-in implementation. InetAddressResolverProvider is the service provider class. Its API docs provide all the details on this mechanism.

InetAddress Caching

The InetAddress class has a cache to store successful as well as unsuccessful host name resolutions.

By default, when a security manager is installed, in order to protect against DNS spoofing attacks, the result of positive host name resolutions are cached forever. When a security manager is not installed, the default behavior is to cache entries for a finite (implementation dependent) period of time. The result of unsuccessful host name resolution is cached for a very short period of time (10 seconds) to improve performance.

If the default behavior is not desired, then a Java security property can be set to a different Time-to-live (TTL) value for positive caching. Likewise, a system admin can configure a different negative caching TTL value when needed or extend the usage of the stale data.

Three Java security properties control the TTL values used for positive and negative host name resolution caching:

networkaddress.cache.ttl
Indicates the caching policy for successful name lookups from the name service. The value is specified as an integer to indicate the number of seconds to cache the successful lookup. The default setting is to cache for an implementation specific period of time.

A value of -1 indicates "cache forever".

networkaddress.cache.stale.ttl
Indicates the caching policy for stale names. The value is specified as an integer to indicate the number of seconds that stale names will be kept in the cache. A name is considered stale if the TTL has expired and an attempt to lookup the host name again was not successful. This property is useful if it is preferable to use a stale name rather than fail due to an unsuccessful lookup. The default setting is to cache for an implementation specific period of time.

If the value of this property is larger than "networkaddress.cache.ttl" then "networkaddress.cache.ttl" will be used as a refresh interval of the name in the cache. For example, if this property is set to 1 day and "networkaddress.cache.ttl" is set to 30 seconds, then the positive response will be cached for 1 day but an attempt to refresh it will be done every 30 seconds.

A value of 0 (zero) or if the property is not set means do not use stale names. Negative values are ignored.

networkaddress.cache.negative.ttl (default: 10)
Indicates the caching policy for un-successful name lookups from the name service. The value is specified as an integer to indicate the number of seconds to cache the failure for un-successful lookups.

A value of 0 indicates "never cache". A value of -1 indicates "cache forever".

Author
Chris Warth
Since
1.0
External Specification
https://www.rfc-editor.org/info/rfc1918, https://www.rfc-editor.org/info/rfc2365, https://www.rfc-editor.org/info/rfc2373, https://www.rfc-editor.org/info/rfc790
See Also
java.net.InetAddress#getByAddress(byte[]), java.net.InetAddress#getByAddress(java.lang.String, byte[]), java.net.InetAddress#getAllByName(java.lang.String), java.net.InetAddress#getByName(java.lang.String), java.net.InetAddress#getLocalHost()

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static interface
private static class
private static class
InetAddress.CachedLookup

A cached result of a name service lookup.

private static class
InetAddress.HostsFileResolver

The HostsFileResolver provides host address mapping by reading the entries in a hosts file, which is specified by jdk.net.hosts.file system property

The file format is that which corresponds with the /etc/hosts file IP Address host alias list.

pack-priv static class
private static class
private static class
InetAddress.PlatformResolver

The default InetAddressResolver implementation, which delegates to the underlying OS network libraries to resolve host address mappings.

private static class
InetAddress.ValidCachedLookup

A cached valid lookup containing addresses whose validity may be temporarily extended by an additional stale period pending the mapping being refreshed or updated.

Field Summary

Modifier and TypeField and Description
private static volatile InetAddressResolver
private static final InetAddressResolver
private static final ConcurrentMap<String, InetAddress.Addresses>
private static volatile InetAddress.CachedLocalHost
private transient String
canonicalHostName

Used to store the best available hostname.

private static final NavigableSet<InetAddress.CachedLookup>
private static final long
pack-priv final transient InetAddress.InetAddressHolder
private static final String
pack-priv static final InetAddressImpl
private static final RuntimePermission
INET_ADDRESS_RESOLVER_PERMISSION

The RuntimePermission("inetAddressResolverProvider") is necessary to subclass and instantiate the InetAddressResolverProvider class, as well as to obtain resolver from an instance of that class, and it is also required to obtain the operating system name resolution configurations.

pack-priv static final int
IPv4

Specify the address family: Internet Protocol, Version 4

pack-priv static final int
IPv6

Specify the address family: Internet Protocol, Version 6

pack-priv static final InetAddressResolver.LookupPolicy
PLATFORM_LOOKUP_POLICY

Platform-wide LookupPolicy initialized from "java.net.preferIPv4Stack", "java.net.preferIPv6Addresses" system properties.

private static final String
private static final String
private static volatile InetAddressResolver
private static final ReentrantLock
private static final ObjectStreamField[]
private static final long
serialVersionUID

use serialVersionUID from JDK 1.0.2 for interoperability

private static final Unsafe

Constructor Summary

AccessConstructor and Description
pack-priv
InetAddress()

Constructor for the Socket.accept() method.

Method Summary

Modifier and TypeMethod and Description
pack-priv static InetAddress
private static InetAddressResolverProvider.Configuration
private static InetAddressResolver

Returns:

an InetAddressResolver
createBuiltinInetAddressResolver
()

Create an instance of the InetAddressResolver interface based on the setting of the jdk.net.hosts.file system property.

public boolean

Returns:

true if the objects are the same; false otherwise.
equals
(Object
the object to compare against.
obj
)

Overrides java.lang.Object.equals.

Compares this object against the specified object.

public byte[]

Returns:

the raw IP address of this object.
getAddress
()

Returns the raw IP address of this InetAddress object.

pack-priv static InetAddress[]
public static InetAddress[]

Returns:

an array of all the IP addresses for a given host name.
getAllByName
(String
the name of the host, or null.
host
)

Given the name of a host, returns an array of its IP addresses, based on the system-wide resolver.

pack-priv static InetAddress[]
getAllByName0(String host, boolean check)

package private so SocketPermission can call it

private static InetAddress[]

Returns:

array of InetAddress(es)
getAllByName0
(String
host name to look up
host
,
boolean
perform security check
check
,
boolean
use cached value if not expired else always perform name service lookup (and cache the result)
useCache
)

Designated lookup method.

public static InetAddress

Returns:

an InetAddress object created from the raw IP address.
getByAddress
(String
the specified host
host
,
byte[]
the raw IP address in network byte order
addr
)

Creates an InetAddress based on the provided host name and IP address.

public static InetAddress

Returns:

an InetAddress object created from the raw IP address.
getByAddress
(byte[]
the raw IP address in network byte order
addr
)

Returns an InetAddress object given the raw IP address .

public static InetAddress

Returns:

an IP address for the given host name.
getByName
(String
the specified host, or null.
host
)

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

public String

Returns:

the fully qualified domain name for this IP address. If either the operation is not allowed by the security check or the system-wide resolver wasn't able to determine the fully qualified domain name for the IP address, the textual representation of the IP address is returned instead.
getCanonicalHostName
()

Gets the fully qualified domain name for this IP address using the system-wide resolver.

public String

Returns:

the raw IP address in a string format.
getHostAddress
()

Returns the IP address string in textual presentation.

private static String

Returns:

the fully qualified domain name for the given IP address. If either the operation is not allowed by the security check or the system-wide resolver wasn't able to determine the fully qualified domain name for the IP address, the textual representation of the IP address is returned instead.
getHostFromNameService
(InetAddress addr, boolean
make security check if true
check
)

Returns the fully qualified domain name for the given address.

public String

Returns:

the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.
getHostName
()

Gets the host name for this IP address.

pack-priv String

Returns:

the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.
getHostName
(boolean
make security check if true
check
)

Returns the hostname for this address.

public static InetAddress

Returns:

the address of the local host.
getLocalHost
()

Returns the address of the local host.

public static InetAddress

Returns:

the InetAddress loopback instance.
getLoopbackAddress
()

Returns the loopback address.

public int

Returns:

a hash code value for this IP address.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hashcode for this IP address.

pack-priv InetAddress.InetAddressHolder
private static native void
init()

Perform class load-time initializations.

private static final InetAddressResolver.LookupPolicy
initializePlatformLookupPolicy()

Creates an address lookup policy from "java.net.preferIPv4Stack", "java.net.preferIPv6Addresses" system property values, and O/S configuration.

private static UnknownHostException
invalidIPv6LiteralException(String host, boolean wrapInBrackets)

pack-priv static boolean
ipv4AddressesFirst(int lookupCharacteristics)

pack-priv static boolean
ipv6AddressesFirst(int lookupCharacteristics)

public boolean

Returns:

a boolean indicating if the InetAddress is a wildcard address.
isAnyLocalAddress
()

Utility routine to check if the InetAddress is a wildcard address.

private static native boolean
private static native boolean
public boolean

Returns:

a boolean indicating if the InetAddress is a link local address; or false if address is not a link local unicast address.
isLinkLocalAddress
()

Utility routine to check if the InetAddress is a link local address.

public boolean

Returns:

a boolean indicating if the InetAddress is a loopback address; or false otherwise.
isLoopbackAddress
()

Utility routine to check if the InetAddress is a loopback address.

public boolean

Returns:

a boolean indicating if the address has is a multicast address of global scope, false if it is not of global scope or it is not a multicast address
isMCGlobal
()

Utility routine to check if the multicast address has global scope.

public boolean

Returns:

a boolean indicating if the address has is a multicast address of link-local scope, false if it is not of link-local scope or it is not a multicast address
isMCLinkLocal
()

Utility routine to check if the multicast address has link scope.

public boolean

Returns:

a boolean indicating if the address has is a multicast address of node-local scope, false if it is not of node-local scope or it is not a multicast address
isMCNodeLocal
()

Utility routine to check if the multicast address has node scope.

public boolean

Returns:

a boolean indicating if the address has is a multicast address of organization-local scope, false if it is not of organization-local scope or it is not a multicast address
isMCOrgLocal
()

Utility routine to check if the multicast address has organization scope.

public boolean

Returns:

a boolean indicating if the address has is a multicast address of site-local scope, false if it is not of site-local scope or it is not a multicast address
isMCSiteLocal
()

Utility routine to check if the multicast address has site scope.

public boolean

Returns:

a boolean indicating if the InetAddress is an IP multicast address
isMulticastAddress
()

Utility routine to check if the InetAddress is an IP multicast address.

public boolean

Returns:

a boolean indicating if the address is reachable.
isReachable
(int
the time, in milliseconds, before the call aborts
timeout
)

Test whether that address is reachable.

public boolean

Returns:

a boolean indicating if the address is reachable.
isReachable
(NetworkInterface
the NetworkInterface through which the test will be done, or null for any interface
netif
,
int
the maximum numbers of hops to try or 0 for the default
ttl
,
int
the time, in milliseconds, before the call aborts
timeout
)

Test whether that address is reachable.

public boolean

Returns:

a boolean indicating if the InetAddress is a site local address; or false if address is not a site local unicast address.
isSiteLocalAddress
()

Utility routine to check if the InetAddress is a site local address.

private static InetAddressResolver
public static InetAddress

Returns:

an InetAddress object with no hostname set, and constructed from the provided IP address literal.
ofLiteral
(String
the textual representation of an IP address.
ipAddressLiteral
)

Creates an InetAddress based on the provided textual representation of an IP address.

private void
readObject(ObjectInputStream
the ObjectInputStream from which data is read
s
)

Restores the state of this object from the stream.

private Object

Returns:

the alternate object to the de-serialized object.
readResolve
()

Replaces the de-serialized object with an Inet4Address object.

private static InetAddressResolver
pack-priv static boolean
systemAddressesOrder(int lookupCharacteristics)

public String

Returns:

a string representation of this IP address.
toString
()

Overrides java.lang.Object.toString.

Converts this IP address to a String.

private static void
private void
writeObject(ObjectOutputStream
the ObjectOutputStream to which data is written
s
)

Writes the state of this object to the stream.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait