Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web Development
Expertise: Beginner
Nov 5, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Incorrect IP Lookup

I have an application which uses dialup networking. If I connect after starting my application, InetAddress.getLocalHost() always returns Even if I connect before starting up, then disconnect and reconnect, the IP address I was assigned on the first connection by PPP is returned and not the new one. Why is this happening?

In the past, I have taken some jabs at the java.net package, either from a design perspective or criticizing the JDK implementation. You've just run into another poorly conceived implementation detail. The InetAddress class in JDKs 1.0.2 and 1.1.x misguidedly caches hostname lookups in a Hashtable. When a lookup is performed, the cache is checked first. If the entry is not in the cache, native DNS resolution is performed.

The flaw with this implementation is that it leads to exactly the problem you've encountered. If the hostname to IP address mapping of the local host changes, it will not be reported. In principle, it may be all right to do this because DNS servers cache mappings for set periods of time and will not instantly reflect changes. But long-lived Java applications will become victims of out-dated information stored in their Hashtable. JDK 1.2 tries to improve the situation by storing cache entries in a HashMap and giving each cache entry an expiration time.

However, the fundamental problem is still there. Rather than provide an extra layer of name resolution caching in InetAddress, the JDK implementation should rely on the native name resolution mechanisms that already have performance optimizations built in. In my opinion, you're dealing with a bug and the only way around it is to fiddle with the JDK classes. The JDK 1.2 implementation should at least give you control over the caching policy and expiration time, but it doesn't.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date