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

How to send ICMP packet in JAVA

839 views
Skip to first unread message

Jingui Yang

unread,
Apr 4, 2002, 5:02:55 AM4/4/02
to
Dear all:
How to send ICMP packet in JAVA,and how to program with
"native"?(such as C,C++)

import java.net.*;

public class ICMP
{
static
{
System.loadLibrary( "icmplib" );
}

private int m_socket;

public ICMP( ) throws SocketException
{
initialize();
m_socket = createSocket();
}

public boolean sendPing ( InetAddress host, int timeout )
{
try
{
sendICMP( m_socket,
host.getHostAddress(),
System.currentTimeMillis() );

return (recvICMP( m_socket, timeout ) != 0 );
} catch ( Exception e )
{
return false;
}
}

public void finalize( )throws Throwable
{
closeSocket( m_socket );
super.finalize();
}

private static native long initialize( ) throws SocketException;

private static native int createSocket( );

private static native long sendICMP( int socket,
String host,
long ID);

private static native long recvICMP ( int socket, long timeout );

private static native boolean closeSocket( int socket );
}


import java.net.*;
public class Ping
{

public static void main (String[] args)
{
if ( args.length > 0 )
{
try
{
ICMP ping = new ICMP();
for ( int argCounter = 0; argCounter < args.length;
argCounter++ )
{
try
{
for ( int i = 0; i < 4; i++ )
{
InetAddress addr = InetAddress.getByName(
args[argCounter] );
System.out.print ( "Sending " + addr +
"... " );
if ( ping.sendPing ( addr, 5000 ) == true
)
{
System.out.println ( "Reply" );
} else
{
System.out.println ( "Time out" );
}
} // for i
} catch ( UnknownHostException e )
{
System.out.println ( "Unknown host: " +
args[argCounter] );
}
} // for argCounter
} catch ( SocketException e )
{
System.out.println ( e.getMessage() );
} // catch
} else
{
System.out.println ( "ping <hostname>" );
}
}

}

When I do this: java Ping 202.114.39.1
there are errors:
Exception in thread "main" java.lang.UnsatisfiedLinkError:no icmplib
in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at ICMP.<clinit>(ICMP.java:7)
at Ping.main(Ping.java:13)
In the same directory,there are some other files:
icmplib.c,icmplib.def,icmplib.dsp,icmplib.dsw,native.h

Gordon Beaton

unread,
Apr 4, 2002, 5:53:03 AM4/4/02
to
On 4 Apr 2002 02:02:55 -0800, Jingui Yang wrote:
> How to send ICMP packet in JAVA

Java has no support for sending or receiving ICMP packets.

> ,and how to program with "native"?(such as C,C++)

Have a look at the JNI spec and tutorial:

http://java.sun.com/j2se/1.4/docs/guide/jni/index.html

If it's this error you are asking about:

> Exception in thread "main" java.lang.UnsatisfiedLinkError:no icmplib
> in java.library.path

then it means that the native library ("libicmplib.so" or
"icmplib.dll", depending on your OS) could not be found in the
LD_LIBRARY_PATH or PATH (again, depending on your OS).

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

0 new messages