Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9148 > unrolled thread

Ping from JAVA to IP Address

Started bysahm <sahm007@gmail.com>
First post2011-10-24 11:01 -0700
Last post2011-11-06 15:36 -0500
Articles 14 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Ping from JAVA to IP Address sahm <sahm007@gmail.com> - 2011-10-24 11:01 -0700
    Re: Ping from JAVA to IP Address Robert Klemme <shortcutter@googlemail.com> - 2011-10-24 20:26 +0200
      Re: Ping from JAVA to IP Address markspace <-@.> - 2011-10-24 14:52 -0700
        Re: Ping from JAVA to IP Address sahm <sahm007@gmail.com> - 2011-10-24 22:31 -0700
          Re: Ping from JAVA to IP Address sahm <sahm007@gmail.com> - 2011-10-25 01:56 -0700
            Re: Ping from JAVA to IP Address Screamin Lord Byron <scre@min.dot> - 2011-10-25 11:57 +0200
            Re: Ping from JAVA to IP Address markspace <-@.> - 2011-10-25 08:12 -0700
              Re: Ping from JAVA to IP Address Screamin Lord Byron <scre@min.dot> - 2011-10-25 22:13 +0200
                Re: Ping from JAVA to IP Address markspace <-@.> - 2011-10-25 16:02 -0700
                  Re: Ping from JAVA to IP Address Screamin Lord Byron <scre@min.dot> - 2011-10-26 13:16 +0200
                Re: Ping from JAVA to IP Address Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 15:29 -0500
                  Re: Ping from JAVA to IP Address glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2011-12-07 08:59 +0000
    Re: Ping from JAVA to IP Address Roedy Green <see_website@mindprod.com.invalid> - 2011-10-25 21:28 -0700
    Re: Ping from JAVA to IP Address Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 15:36 -0500

#9148 — Ping from JAVA to IP Address

Fromsahm <sahm007@gmail.com>
Date2011-10-24 11:01 -0700
SubjectPing from JAVA to IP Address
Message-ID<670cd44b-d05f-4300-9c59-b26f28e17323@j36g2000prh.googlegroups.com>
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

[toc] | [next] | [standalone]


#9149

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-10-24 20:26 +0200
Message-ID<9gloupFf3oU1@mid.individual.net>
In reply to#9148
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/

[toc] | [prev] | [next] | [standalone]


#9154

Frommarkspace <-@.>
Date2011-10-24 14:52 -0700
Message-ID<j84mmv$6b7$1@dont-email.me>
In reply to#9149
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.


[toc] | [prev] | [next] | [standalone]


#9160

Fromsahm <sahm007@gmail.com>
Date2011-10-24 22:31 -0700
Message-ID<0aec9e49-b7d2-4afd-9f8f-07a2d07392ca@k13g2000prg.googlegroups.com>
In reply to#9154
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

[toc] | [prev] | [next] | [standalone]


#9166

Fromsahm <sahm007@gmail.com>
Date2011-10-25 01:56 -0700
Message-ID<26b57282-aa91-4116-884c-92af6ae1f911@k38g2000pro.googlegroups.com>
In reply to#9160
On Oct 25, 8:31 am, sahm <sahm...@gmail.com> wrote:
> 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

All I want to do is simple function that can check if there is
Internet connection or Not ?

Best
Salim

[toc] | [prev] | [next] | [standalone]


#9169

FromScreamin Lord Byron <scre@min.dot>
Date2011-10-25 11:57 +0200
Message-ID<j8615n$icu$1@news.metronet.hr>
In reply to#9166
On 25.10.2011 10:56, sahm wrote:
> On Oct 25, 8:31 am, sahm <sahm...@gmail.com> wrote:
>> 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
> 
> All I want to do is simple function that can check if there is
> Internet connection or Not ?

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

[toc] | [prev] | [next] | [standalone]


#9174

Frommarkspace <-@.>
Date2011-10-25 08:12 -0700
Message-ID<j86jl3$tvd$1@dont-email.me>
In reply to#9166
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");
       }
    }
}

[toc] | [prev] | [next] | [standalone]


#9178

FromScreamin Lord Byron <scre@min.dot>
Date2011-10-25 22:13 +0200
Message-ID<wlhabwd73x60$.1qs46lbqszfoq.dlg@40tude.net>
In reply to#9174
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

[toc] | [prev] | [next] | [standalone]


#9183

Frommarkspace <-@.>
Date2011-10-25 16:02 -0700
Message-ID<j87f5m$12g$1@dont-email.me>
In reply to#9178
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.




[toc] | [prev] | [next] | [standalone]


#9202

FromScreamin Lord Byron <scre@min.dot>
Date2011-10-26 13:16 +0200
Message-ID<j88q75$8d5$1@news.metronet.hr>
In reply to#9183
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.

[toc] | [prev] | [next] | [standalone]


#9667

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-06 15:29 -0500
Message-ID<4eb6ee10$0$290$14726298@news.sunsite.dk>
In reply to#9178
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

[toc] | [prev] | [next] | [standalone]


#10582

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2011-12-07 08:59 +0000
Message-ID<jbn9te$dab$2@speranza.aioe.org>
In reply to#9667
Arne Vajhøj <arne@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

[toc] | [prev] | [next] | [standalone]


#9193

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-10-25 21:28 -0700
Message-ID<n23fa750va518i4ubhfrprf6a9lprthlkr@4ax.com>
In reply to#9148
On Mon, 24 Oct 2011 11:01:27 -0700 (PDT), sahm <sahm007@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.

[toc] | [prev] | [next] | [standalone]


#9669

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-06 15:36 -0500
Message-ID<4eb6efb9$0$295$14726298@news.sunsite.dk>
In reply to#9148
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web