Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I use InetAddress.isReachable() when DNS is unavailable?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    Norway
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I use InetAddress.isReachable() when DNS is unavailable?

    I'm making an ISP logger, that fires an event when I loose internet connectivity.
    The trigger is when I no longer can resolve "www.google.com", which in turn starts a network diagnose process.
    My idea is to ping my router, the modem and an external ip, to try to pinpoint where the problem is.

    The problem I'm facing is that creating an InetAddress with getByName("192.168.0.1") is of no use when the DNS is no longer available:
    InetAddress ia = InetAddress.getByName("192.168.0.1");
    if (ia.isReachable(1000)) {
      System.out.println("This works if the DNS server is available");
    } else {
      System.out.println("But it doesn't if DNS is not available.");
      System.out.println("Although my reason for trying to ping the IP is exactly the reason it's not working:");
      System.out.println("The DNS server is no longer responding!");
    }

    So my obvious question is how do I do this without a DNS (or host file) present?
    Would converting a String "192.168.0.1" to a byte array {192, 168, 0, 1} and using getByAddress() work?
    byte addr[] = {(byte) 192, (byte) 168, 0, 1};
    InetAddress ia = InetAddress.getByAddress(addr);
    if (ia.isReachable(1000)) {
      System.out.println("Host is reachable, but will this be true if DNS isn't available?");
    }

    Martin
    Last edited by TylerD75; September 10th, 2014 at 08:13 PM. Reason: Guess I had to add some code to make everyone happy ;)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I use InetAddress.isReachable() when DNS is unavailable?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    Norway
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I use InetAddress.isReachable() when DNS is unavailable?

    Never mind, I figured out I had some other problems elsewhere in my code, that could lead to the problem.
    But I also used a tokenizer to split a string IP into a byte array, which might also have solved the problem.
    ...too lazy to go back and check what really caused this though

Similar Threads

  1. "Services with missing/unavailable dependencies
    By Namrata@nanowebtech in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 10th, 2013, 05:51 AM
  2. NetworkInterface vs InetAddress
    By keepStriving in forum Java Networking
    Replies: 0
    Last Post: June 5th, 2013, 12:03 PM
  3. InetAddress
    By lanpan in forum Member Introductions
    Replies: 2
    Last Post: December 13th, 2011, 04:12 AM
  4. [SOLVED] InetAddress worn't work :(
    By Budlee in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2011, 10:10 AM
  5. DNS Resolution
    By nim.izadi in forum Java Theory & Questions
    Replies: 0
    Last Post: November 6th, 2010, 02:25 PM