Search code examples
javasocketsnetwork-programmingdnsnio

Does Java Socket cache DNS?


The ctor for Socket has versions that take the server as either a string or InetAddress. Are the results of the DNS lookup cached, so that another constructed Socket will skip performing the lookup and use the cached value, if available? And if so, does this apply to both versions, or just the one taking an InetAddress?


Solution

  • Does Java Socket cache DNS?

    No, but InetAddress does, or something under its hood, and Socket uses InetAddress to resolve names, so in effect yes.

    See the Java Networking Properties, networkaddress.cache.ttl and friends.