View previous topic :: View next topic |
Author |
Message |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Mon Aug 30, 2004 10:47 pm Post subject: Java Ping |
|
|
Hi all,
After doing a little research on google its seems that the standard Java networking libraries do not support ICMP ping. I have read this is because ICMP packets can only be created by a socket of the SOCK_RAW type. So does anyone know of alternatives that would give me this functionality. Presently I'm aware that the Runtime class could be used something like this:
Code: |
Process prog = Runtime.getRuntime().exec("ping.exe IP_ADDRESS");
BufferedReader br = new BufferedReader(new InputStreamReader(prog.getInputStream()));
while((inputLine = br.readLine()) != null)
{
System.out.println(inputLine);
} |
Yet this really walks over the fact that Java is platform independent by running a windows program, thefore could some sort of open source packet driver be the answer? I would really appreciate if someone good point me in the right direction or just explain why Sun haven't added this functionality or whether it's in the pipeline.
|
|
Back to top |
|
 |
Dunceor Just Arrived

Joined: 05 Sep 2003 Posts: 4 Location: Sweden

|
Posted: Mon Aug 30, 2004 11:15 pm Post subject: |
|
|
I don't know in java but if you are using C there is a great lib called libnet which makes it dead easy to create ICMP packets.
Libnet: http://libnet.sourceforge.net/
|
|
Back to top |
|
 |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Tue Aug 31, 2004 1:47 am Post subject: |
|
|
Thanks for the quick reply Dunceor. But unfortunately I'm only familiar with Java because I've only recently started to program. But perhaps by the sounds of it I should learn another language like C if I want this sort of functionality. Unless someone else has some thoughts on the matter.
|
|
Back to top |
|
 |
el-half Just Arrived


Joined: 01 Jan 2004 Posts: 0 Location: Cyberspace

|
Posted: Tue Aug 31, 2004 8:51 am Post subject: |
|
|
Maybe this can be useful: http://sourceforge.net/projects/jpcap
I've never used it so I don't know what its possibilities are but it might be what you are looking for.
|
|
Back to top |
|
 |
sequru Just Arrived

Joined: 31 Jul 2004 Posts: 0

|
Posted: Tue Aug 31, 2004 9:34 am Post subject: |
|
|
Hi,
If you want to use ping in order to find out whether the host alive or not, then you can use Sockets. Here is what I have found from http://www.torsten-horn.de/techdocs/java-net.htm
Code: |
import java.net.*;
public class PingSimple
{
public static void main( String[] args )
{
InetAddress iaHost = null;
int iPort = 80;
System.out.println( "\nUsage: java PingSimple host [port] \n" +
" e.g.: java PingSimple www.Torsten-Horn.de \n" +
" or: java PingSimple 1.2.3.4 80 \n" );
try
{
if( 0 >= args.length )
throw new Exception( "Parameter missing!" );
iaHost = InetAddress.getByName( args[0] );
if( 1 < args.length )
iPort = Integer.parseInt( args[1] );
long tm = System.currentTimeMillis();
Socket so = new Socket( iaHost, iPort );
so.close();
tm = System.currentTimeMillis() - tm;
System.out.println( "Connection ok (port " + iPort + ", time = " + tm + " ms). \n" +
"Host Address = " + iaHost.getHostAddress() + "\n" +
"Host Name = " + iaHost.getHostName() );
System.exit( 0 );
} catch( Exception ex )
{
System.out.println( "Error: " + ex.getMessage() );
System.exit( 1 );
}
}
}
|
|
|
Back to top |
|
 |
[[Merlin]] Just Arrived


Joined: 16 Dec 2003 Posts: 3 Location: United Kingdom

|
Posted: Wed Sep 01, 2004 12:39 am Post subject: |
|
|
Hi,
Firstly thanks to both el-half and sequru for your input as it gives me two futher avenues to explore. Although after reading sequru's link its does confirm my first post that ICMP ping is not directly implemented in Java. So does anybody know why it has not been included, is it someone to do with how the virtual machine is structured or just Sun bureaucracy.
|
|
Back to top |
|
 |
el-half Just Arrived


Joined: 01 Jan 2004 Posts: 0 Location: Cyberspace

|
Posted: Wed Sep 01, 2004 6:13 pm Post subject: |
|
|
The Java platform is secure so they didn't implement raw sockets. At least, that is what I think is the reason.
|
|
Back to top |
|
 |
eobyn Just Arrived

Joined: 02 Sep 2004 Posts: 0

|
Posted: Fri Sep 03, 2004 6:55 pm Post subject: |
|
|
You can use a C library to ping and create a JNI native method map between C and JAVA. I don't think there is a way to create ICMP packets included in java API, not sure though.
|
|
Back to top |
|
 |
yazid Just Arrived


Joined: 13 Oct 2004 Posts: 0

|
|
Back to top |
|
 |
|