Networking code sometimes throws UnknownHostException
Asked Answered
M

5

7

I am trying to data from a server. Sometimes my code fails due to an UnknownHostException. Why is that? What is the cause of this problem?

Meemeece answered 25/5, 2010 at 17:2 Comment(1)
An UnknownHostException is thrown if a java.net.UnknownHostException occurs while creating a connection to the remote host for a remote method call. Something isn't resolving at the DNS level this isn't a java or an xml problem.Dagnah
Z
5

This may occur if a hiccup in DNS server has occurred. Apart from making the DNS server more robust or looking for another one, you can also just use the full IP address instead of the hostname. This way it doesn't need to lookup the IP address based on the hostname. However, I would rather fix the DNS issue and prefer the DNS since IP addresses may change from time to time.

Zolnay answered 25/5, 2010 at 17:5 Comment(2)
Thanks BalusC :). Great, you gave the ip. Now the DNS itself doesn't come into picture. But it's not working. Now i get this error :- java.io.FileNotFoundException: 216.115.98.240/rss/indiaMeemeece
Even when i type "216.115.98.240/rss/india" i get nothing. Is this ip correct? When i did ping i got this ip 216.115.97.236. However in this also i get the same exception in java and in browser also nothing displays.Meemeece
M
2

An UnknownHostException indicates the host specified couldn't be translated to an IP address. It could very well be a problem with your DNS server.

Millepede answered 25/5, 2010 at 17:6 Comment(4)
Ok MarkPeters, so how should i resolve this? since i have to submit my work in college and i don't think i will able to have access to college's router and change the dns server in it :(Meemeece
@Nitesh: Is it failing in the college's environment or only in your local environment? If it's inconsistently failing from the college's environment, I would both notify your teacher or tutor that there may be an environment problem that's out of your control, and also try to lobby your college IT staff to diagnose and fix the problem. You might consider an approach where you catch the exception and try again a few times, perhaps a few seconds/minutes apart.Millepede
//Override system DNS setting with Google free DNS server System.setProperty("sun.net.spi.nameservice.nameservers", "8.8.8.8"); System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");Capitalization
In browser DNS translation happens for me, but only in java app I face this problem.Astrid
C
1

If the DNS resolution fails intermittently, catch the exception and try again until you get name resolution. You can only control, what you can control... And if you can't control/fix the DNS server, make your app robust enough to handle the quirky DNS server.

Catchascatchcan answered 22/8, 2017 at 13:4 Comment(0)
S
1

i used the example of this post and in my case was the java version. With jdk1.6 fails, but with a newer one it didn't have any problem.

https://forums.freebsd.org/threads/java-problem-inetaddress-getlocalhost-is-not-working.26618/

Simaroubaceous answered 2/9, 2023 at 17:53 Comment(0)
H
0

I too am seeing sporadic UnknownHostExceptions in Java for no apparent reason. The solution is just to retry a few times. Here is a wrapper for DocumentBuilder.parse that does this:

static Document DocumentBuilder_parse(DocumentBuilder b, String uri) throws SAXException, IOException {
  UnknownHostException lastException = null;
  for (int tries = 0; tries < 2; tries++) {
    try {
      return b.parse(uri);
    } catch (UnknownHostException e) {
      lastException = e;
      System.out.println("Retrying because of: " + e);
      continue;
    }
  }
  throw lastException;
}
Hecate answered 6/4, 2019 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.