Java Ping

Networking/Security Forums -> Programming and More

Author: [[Merlin]]Location: United Kingdom PostPosted: 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.

Author: DunceorLocation: Sweden PostPosted: 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/

Author: [[Merlin]]Location: United Kingdom PostPosted: 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.

Author: el-halfLocation: Cyberspace PostPosted: 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.

Author: sequru PostPosted: 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 );
      }
    }
  }


Author: [[Merlin]]Location: United Kingdom PostPosted: 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.

Author: el-halfLocation: Cyberspace PostPosted: 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.

Author: eobyn PostPosted: 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.

Author: yazid PostPosted: Wed Oct 13, 2004 9:55 pm    Post subject: java ping
    ----
hi, I had the same problem and then i found this site which answers your question. http://www.geocities.com/SiliconValley/Bit/5716/ping/index_eng.html
the implentation is for Windows.
Good luck! Smile



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group