views:

611

answers:

2

I have a device on a network that I am attempting to ping through my Java program. Through my windows command prompt, I can ping the device address fine and do a tracert on the address fine.

Online, I have seen that in order to do a ping through Java you have to do the following:

InetAddress.getByName(address).isReachable(timeout);

But, when I use this code on my device address, it always returns false in my program. I am using the correct IPv4 address with a good timeout value. Also, if I use a localhost address, it works fine.

Why can I ping the device through cmd, but not through my program? I have heard in various places that this is not a true ping.

Is there a better way to emulate a ping in Java?

Thanks

A: 

Here is written that isReacheble is real ICMP request (the same way ping works). So it is best way i think. In the end of same article is written that if ICMP fails java may try different ways to check availability.

Andrey
+4  A: 

isReachable() will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
Thus your problem is probably a configuration issue of not enough permissions to do this on the client machine or a port 7 issue on the server if your client doesn't have permission to do the ICMP ECHO REQUEST. Probably both in your case, you need to resolve one side or the other to get this to work.

I tested the following on OSX and Linux clients and it works when testing for reachablity of other OSX, Linux and Windows Server machines. I don't have a Windows machine to run this as a client.

import java.io.IOException;
import java.net.InetAddress;

public class IsReachable
{
    public static void main(final String[] args) throws IOException
    {
        final InetAddress host = InetAddress.getByName(args[0]);
        System.out.println("host.isReachable(1000) = " + host.isReachable(1000));
    }
}

from what I read here. It is apparently a Windows limitation and ICMP PING isn't supported on Windows as a system call previous to Windows 2000, so it defaults to try and connect to Port 7 and that is blocked on the machine you are trying to "reach". Java doesn't support the new native system call yet. The permissions thing is for Unix based system as they require root to send ICMP packets.

If you want to roll your own Windows native JNI ICMP PING for Windows 2000 and newer there is the IcmpSendEcho Function.

fuzzy lollipop
... but I can do it through Windows command prompt using the standard ping command.
stjowa
Check your java.policy file and other security settings, probably under the lib/security directory of your java home.
crowne
What permissions/configurations am I looking for? And what do I change it to?
stjowa
you need to be root under a Unix environment to send ICMP packets. But it looks like you are using Windows.
fuzzy lollipop
@stjowa : yes, but you cannot use the system call
Valentin Rocher