Ping from JAVA to IP Address

S

sahm

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
 
R

Robert Klemme

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
 
S

sahm

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
 
S

sahm

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
 
M

markspace

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");
}
}
}
 
S

Screamin Lord Byron

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
 
M

markspace

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

Roedy Green

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

Screamin Lord Byron

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

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
G

glen herrmannsfeldt

(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top