Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ping from JAVA to IP Address

1,834 views
Skip to first unread message

sahm

unread,
Oct 24, 2011, 2:01:27 PM10/24/11
to
Hi every one

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 ===============

Best
Salim

Robert Klemme

unread,
Oct 24, 2011, 2:26:22 PM10/24/11
to
On 24.10.2011 20:01, 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

Does a command line ping work? If not you cannot expect it to work from
Java. There might be firewalls in between blocking ICMP.

> ============ 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);

AFAIK there is no guarantee that isReachable() does a ping:
http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable(int)

> 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 ===============

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

markspace

unread,
Oct 24, 2011, 5:52:30 PM10/24/11
to
On 10/24/2011 11:26 AM, Robert Klemme wrote:

> AFAIK there is no guarantee that isReachable() does a ping:
> http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable(int)


The important part of that document being the phrase "if permission can
be obtained." If you're running in an applet or some other restricted
environment, per mission might be denied to even make the attempt.

I'm curious what a network trace would show.



sahm

unread,
Oct 25, 2011, 1:31:22 AM10/25/11
to
On Oct 25, 12:52 am, markspace <-@.> wrote:
> On 10/24/2011 11:26 AM, Robert Klemme wrote:
>
> > AFAIK there is no guarantee that isReachable() does a ping:
> >http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.htm...)
>
> The important part of that document being the phrase "if permission can
> be obtained."  If you're running in an applet or some other restricted
> environment, per mission might be denied to even make the attempt.
>
> I'm curious what a network trace would show.


Hi
isReachable() is working fine in my local network (192.168.1.0) but
not working in external network

Best
Salim

sahm

unread,
Oct 25, 2011, 4:56:48 AM10/25/11
to
All I want to do is simple function that can check if there is
Internet connection or Not ?

Best
Salim

Screamin Lord Byron

unread,
Oct 25, 2011, 5:57:12 AM10/25/11
to
You'll have to think of another way of doing this, as Java doesn't
support the use of ICMP. :(

See:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4727550

markspace

unread,
Oct 25, 2011, 11:12:30 AM10/25/11
to
On 10/25/2011 1:56 AM, sahm wrote:
> All I want to do is simple function that can check if there is
> Internet connection or Not ?


Doesn't work for me either, and I'm not sure why. Try opening an HTTP
socket on a well known address. Can anybody supply a public host name
that doesn't talk on port 80 so I can test this works ok when there's no
internet connection?

public class PingTest {

private static String pingVectors[] =
{ "cnn.com", "google.com", };

public static void main(String[] args)
throws UnknownHostException, IOException
{
for( String s : pingVectors ) {
InetAddress ia = InetAddress.getByName(s);
boolean reachable = ia.isReachable(6000);
System.out.println(s+" is reachable: " + reachable);

Socket sock = new Socket( s , 80 );
sock.getOutputStream();
sock.close();
System.out.println(s+" opened socket ok");
}
}
}

Screamin Lord Byron

unread,
Oct 25, 2011, 4:13:47 PM10/25/11
to
On Tue, 25 Oct 2011 08:12:30 -0700, markspace wrote:

> On 10/25/2011 1:56 AM, sahm wrote:
>> All I want to do is simple function that can check if there is
>> Internet connection or Not ?
>
>
> Doesn't work for me either, and I'm not sure why.

Did you read my post? It doesn't work because apparently there is no
support for raw sockets in Java (and according to this bug resolution text,
there will never be). Furthermore, Google doesn't have TCP port 7 open, so
the alternative procedure (tcp connection on port 7) fails too.


Bug ID: 4727550
Votes: 184
Synopsis: Advanced & Raw Socket Support (ICMP, ICMPv6, ping, traceroute,
Category: java:classes_net
Reported Against: 1.3.1 , tiger
Release Fixed:
State: 11-Closed, Will Not Fix, request for enhancement
Priority: 4-Low
Related Bugs: 4526141 , 4740586

markspace

unread,
Oct 25, 2011, 7:02:11 PM10/25/11
to
On 10/25/2011 1:13 PM, Screamin Lord Byron wrote:
>
> Did you read my post? It doesn't work because apparently there is no
> support for raw sockets in Java


I'm not sure the RFE relates to fixing existing functionality like
isReachable(). I read that as a totally different bug, one that was
asking for a brand new, direct API for ICMP. It could be the same as
asking to "fix" isReachable(), but I don't think it's certain at all
that it does.





Roedy Green

unread,
Oct 26, 2011, 12:28:35 AM10/26/11
to
On Mon, 24 Oct 2011 11:01:27 -0700 (PDT), sahm <sah...@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>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.

you cannot literally ping in java. See
http://mindprod.com/jgloss/ping.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.

Screamin Lord Byron

unread,
Oct 26, 2011, 7:16:53 AM10/26/11
to
On 26.10.2011 01:02, markspace wrote:
> On 10/25/2011 1:13 PM, Screamin Lord Byron wrote:
>>
>> Did you read my post? It doesn't work because apparently there is no
>> support for raw sockets in Java
>
>
> I'm not sure the RFE relates to fixing existing functionality like
> isReachable().

It doesn't. Not directly, at least. It's an implementation issue.

> I read that as a totally different bug, one that was
> asking for a brand new, direct API for ICMP. It could be the same as
> asking to "fix" isReachable(), but I don't think it's certain at all
> that it does.

If I understand it correctly, this functionality is no longer
implemented since Java 1.3. The InetAddress class was there since Java
1.0. The documentation states: "A typical implementation will use ICMP
ECHO REQUEST...". But since Java 1.3 the ICMP ECHO REQUEST is no longer
an option. I'm guessing they just couldn't be bothered to change that
documentation line.

It is a reasonable explanation on why the code posted by Salim doesn't
work. It works within the same subnet though, but that's because of the
TCP connection on port 7, not ICMP ping.

The bottom line is, as Roedy put it, you can't ping in Java.

Arne Vajhøj

unread,
Nov 6, 2011, 3:29:05 PM11/6/11
to
On 10/25/2011 4:13 PM, Screamin Lord Byron wrote:
> On Tue, 25 Oct 2011 08:12:30 -0700, markspace wrote:
>> On 10/25/2011 1:56 AM, sahm wrote:
>>> All I want to do is simple function that can check if there is
>>> Internet connection or Not ?
>>
>> Doesn't work for me either, and I'm not sure why.
>
> Did you read my post? It doesn't work because apparently there is no
> support for raw sockets in Java (and according to this bug resolution text,
> there will never be). Furthermore, Google doesn't have TCP port 7 open, so
> the alternative procedure (tcp connection on port 7) fails too.

Well - your post was far from complete.

Raw sockets and ICMP are not exposed to the programmer in Java.

But that does not prevent something inside Java to use ICMP.

The isReachable method will in fact attempt a ping on *nix.

On Windows it uses the echo service.

(it will also fallback to echo on *nix if no privilege to call ping)

Arne

Arne Vajhøj

unread,
Nov 6, 2011, 3:36:08 PM11/6/11
to
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

glen herrmannsfeldt

unread,
Dec 7, 2011, 3:59:26 AM12/7/11
to
Arne Vajhøj <ar...@vajhoej.dk> wrote:

(snip)
> Well - your post was far from complete.

> Raw sockets and ICMP are not exposed to the programmer in Java.

> But that does not prevent something inside Java to use ICMP.

As far as I know, at least for unix-like system, you need to
be setuid root to do raw sockets or ICMP. Running java as
setuid root doesn't sound great for security.

I would think that it could do something like system("ping")
to run the ping program (which is setuid root).

> The isReachable method will in fact attempt a ping on *nix.

> On Windows it uses the echo service.

> (it will also fallback to echo on *nix if no privilege to call ping)

-- glen
0 new messages