Problem with isReachable in InetAddress class
Asked Answered
H

3

11

As an assignment I have to find all the alive computers on a LAN. For which I am using isReachable function of InetAddress class. But problem is that nothing is shown reachable to me. So I tried to have isReachable with Google's IP but still this is unreachable.

Here is the code:

import java.net.*;

public class alive{
    public static void main(String args[]){
        try{
            InetAddress ia = InetAddress.getByAddress(new byte[]{(byte)209, (byte)85, (byte)153, (byte)104});
            boolean b = ia.isReachable(10000);
            if(b){
                System.out.println("Reachable");
            }
            else{
                System.out.println("Unrachable");
            }

        }catch(Exception e){
            System.out.println("Exception: " + e.getMessage());
        }
    }
}

Output is : Unreachable

Hermon answered 24/1, 2011 at 6:46 Comment(7)
have you tried pinging to the adress?Amata
do you have root privileges? if not, try w/...Honeyman
@pangea ya have tried and it is pingingHermon
@Honeyman ya it was a bit surprising that after having root privileges it was reachable but right after without root privileges it wasn't. why so can you please explain it. thanksHermon
read the javadoc, it requires such privileges. Basically to use ICMP (raw socket), it does take 'root'. And if you ask why ping from bash doesn't, actually it does need as well. Do that ls -l /bin/ping :)Honeyman
hate act like wise guy, but the out put from this code will never be "Unreachable"...but "Unrachable" instead :)Fakery
To clarify, it requires privileges to use ICMP (and not Windows). In the other cases it uses TCP to port 7.Rete
A
6

Here are some details on why isReachable() might not always work as expected

  1. http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html
  2. http://www.coderanch.com/t/206934/sockets/java/InetAdress-isReachable-Ping-Permissions

The correct way for you is to use the ICMP protocol. This is what ping uses internatlly, I believe. Here is an example that get you started.

Amata answered 24/1, 2011 at 6:57 Comment(1)
The javaranch link contains an error. isReachable() doesn't require that the echo device be actually running. It interprets connection refusal as a success.Rete
V
4

Here is the code which is platform independent, but requires information about any open port on the other machine (which we have most of the time).

private static boolean isReachable(String addr, int openPort, int timeOutMillis) {
    // Any Open port on other machine
    // openPort =  22 - ssh, 80 or 443 - webserver, 25 - mailserver etc.
    try {
        try (Socket soc = new Socket()) {
            soc.connect(new InetSocketAddress(addr, openPort), timeOutMillis);
        }
        return true;
    } catch (IOException ex) {
        return false;
    }
}
Victoir answered 11/12, 2015 at 18:47 Comment(0)
B
3

I found interesting solution. If you can't run your aplication as root, you may set raw socket capability on java:

sudo setcap cap_net_raw=ep /usr/lib/jvm/jdk/bin/java

And then ICMP protocol will be used istead of echo request on 7 TCP port.

Babita answered 3/7, 2015 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.