Java Reference
In-Depth Information
Caching
Because DNS lookups can be relatively expensive (on the order of several seconds for
a request that has to go through several intermediate servers, or one that's trying to
resolve an unreachable host) the
InetAddress
class caches the results of lookups. Once
it has the address of a given host, it won't look it up again, even if you create a new
InetAddress
object for the same host. As long as IP addresses don't change while your
program is running, this is not a problem.
Negative results (host not found errors) are slightly more problematic. It's not uncom‐
mon for an initial attempt to resolve a host to fail, but the immediately following one
to succeed. The first attempt timed out while the information was still in transit from
the remote DNS server. Then the address arrived at the local server and was immediately
available for the next request. For this reason, Java only caches unsuccessful DNS queries
for 10 seconds.
These times can be controlled by the system properties
networkaddress.cache.ttl
and
networkaddress.cache.negative.ttl
. The first of those,
networkad
dress.cache.ttl
, specifies the number of seconds a successful DNS lookup will remain
in Java's cache.
networkaddress.cache.negative.ttl
is the number of seconds an un‐
successful lookup will be cached. Attempting to look up the same host again within
these limits will only return the same value. Negative 1 is interpreted as “never expire.”
Besides local caching inside the
InetAddress
class, the local host, the local domain name
server, and other DNS servers elsewhere on the Internet also cache the results of various
queries. Java provides no way to control this. As a result, it may take several hours for
the information about an IP address change to propagate across the Internet. In the
meantime, your program may encounter various exceptions, including
UnknownHos
tException
,
NoRouteToHostException
, and
ConnectException
, depending on the
changes made to the DNS.
Lookups by IP address
When you call
getByName()
with an IP address string as an argument, it creates an
InetAddress
object for the requested IP address without checking with DNS. This
means it's possible to create
InetAddress
objects for hosts that don't really exist and
that you can't connect to. The hostname of an
InetAddress
object created from a string
containing an IP address is initially set to that string. A DNS lookup for the actual
hostname is only performed when the hostname is requested, either explicitly via a
getHostName()
. That's how
www.oreilly.com
was determined from the dotted quad ad‐
dress 208.201.239.37. If, at the time the hostname is requested and a DNS lookup is
finally performed, the host with the specified IP address can't be found, the hostname
remains the original dotted quad string. However, no
UnknownHostException
is thrown.
Hostnames are much more stable than IP addresses. Some services have lived at the
same hostname for years, but have switched IP addresses several times. If you have a