0

I want to check all of nodes accessibility in a network using java. I have read This and This Questions and I have write my method by helping these questions, I have at least two tested Ips in my network which are accessible, one of them is 192.168.1.1 and another is 192.168.1.102 you can see test of 192.168.1.1 in the below picture:

enter image description here

Odk but when I run my code my app says that 192.168.1.1 is not reachable. here is the image, I have pointed at it with a red arrow:

enter image description here

ok , and here is my java code:

private void checkNetworkAccessibility(){
        int timeout = 1000;
        String subnet = firstSubnet.getText() + "." + secondSubnet.getText() + "." + thirdSubnet.getText() + ".";
        DefaultTableModel model = (DefaultTableModel)networkTable.getModel();
        for(int i=1;i<=254;i++){            
            try {                
                if(InetAddress.getByName(subnet+i).isReachable(timeout)){
                    model.addRow(new Object[]{subnet + i, subnet + i, "بله", "بله"});
                }
                else{
                    model.addRow(new Object[]{subnet + i, "نامشخص", "خیر", "بله"});
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }

        }
    }

what is wrong with my code?

Community
  • 1
  • 1
Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51
  • Your code is okay. The reason might be the implementation of `isReachable()`. Possible duplicate: [link](http://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address) – Jan Trienes Mar 14 '15 at 16:33
  • @JanTrienes and Thank you buddies, this code really damaged my mind – Behzad Hassani Mar 14 '15 at 16:37

1 Answers1

1

The problem is that .isReachable() is not reliable. Its implementation is totally OS dependent!

Let's take Linux as an example; this method uses the echo TCP service (port 7). Do you actually know of a server which has even that running today? I don't.

It cannot use ping; look at this:

$ ls -l $(which ping)
-rwsr-xr-x 1 root root 44168 May  7  2014 /bin/ping

Yes, that's right; the sticky bit. The reason for this is that the ping command sends an ICMP echo request packet along the wire, and this requires that you be able to access raw sockets.

Which you can't do unless you have the necessary privileges, and which you can't do in Java! Except if you use native libraries. And you run with the necessary privileges. Which more often than not means you need to be admin. Which you don't want.

And of course, a sysadmin may even decide to block incoming echo request packets on a host, so even ping is not reliable...

fge
  • 119,121
  • 33
  • 254
  • 329
  • 1
    Your best bet is to use a process (use a `ProcessBuilder` for that) and use the ping command; however, this remains unreliable in any event. You have to be sure that all the hosts you want to check allow incoming ICMP echo-requests. – fge Mar 14 '15 at 16:40
  • I have test ping method :( but that's result is same as this way – Behzad Hassani Mar 14 '15 at 16:42
  • 1
    You are doing something wrong then; the ping command works. Check that you handle std{in,out,err} etc correctly. – fge Mar 14 '15 at 16:42
  • is it ok for ping? `Process proccess = java.lang.Runtime.getRuntime().exec("ping -w 50 192.168.1.1"); int result = proccess.waitFor();` – Behzad Hassani Mar 14 '15 at 16:44
  • 1
    No. As I said earlier, use a `ProcessBuilder`. – fge Mar 14 '15 at 16:58