How does InetAddress work? How does it find an IP address of a hostname.
I understand we can use InetAddress in Java for DNS resolutions, but I would like to know how it works. How does it find a DNS server to resolve the address?
I recently saw a misconfigured cname, which was not being resolved correctly by InetAddress. It was getting resolved intermittently, but failing most of the times. The dig command succeeded on the same machine all the time. Although after fixing the configuration, InetAddress worked consistently too. But I don't understand why it was succeeding intermittently before. Either it should have failed consistently or succeeded.
According to Javadoc, Java's
InetAddressclass uses the machines network configuration as strategy to resolve a hostname.From http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/net/InetAddress.java or https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/InetAddress.html (The InetAddress source):
This means, that if there are multiple networks, or multiple DNS-servers configured for a single network, you may get varying results in resolution, because different hosts might apply different leniency towards fixing broken addresses.
As to why another application might succees more often depends on their resolution strategy.