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
I saw a lot of bad code written related to that issue. The code that worked for my is (site do not know to correctly parse my code file) :
For an easy ping from java without privileges, I use http://www.icmp4j.org
It's very easy to use :
Using this isn't going to help in case of ping a public IP addresses using Windows machine:
Note: The documentation states that:
I've tried that but the results were not accurate.
What really worked out for me is the class written by our fellow user that send true ICMP ping and returns true or false according to IP status.
Odd InetAddress.isReachable() issue
The following JAVA code is an example of Ping of Death and Denial of Service using Microsoft Windows. This shall be use for testing purpose in order to build an Anti-Hack proof and/or testing the performance of the site in case of similar Cyber attacks.
When tests are completed. You might want to use the following code to clean up the processes in Task Manager.
A bit late, but I stumbled upon this while trying to do the same thing.
One workaround that worked for me and which I used was to just use the command line ping directly.
isReachable()
will useICMP ECHO REQUEST
s 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.
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 sendICMP
packets.If you want to roll your own Windows native JNI
ICMP PING
for Windows 2000 and newer there is the IcmpSendEcho Function.