com.dalsemi.tininet.icmp
Class Ping

java.lang.Object
  |
  +--com.dalsemi.tininet.icmp.Ping

public class Ping
extends Object

Ping provides a TINI Java application the ability to send ICMP echo request and read the raw ICMP response. When a machine receives an ICMP echo request from a remote host it responds with an ICMP echo reply message that contains an exact copy of the request packet.

Ping allows an application to programmatically determine whether a particular service running on a remote host has died or whether the host machine itself has become unreachable on the network.

Usage

Send a single ICMP echo request to a remote host

     InetAddress addr = InetAddress.getByName("www.dalsemi.com");
     boolean response = Ping.pingNode(addr);
     System.out.println(response ? "Got reply from node" : "Node was unreachable");
 


Field Summary
static byte DEFAULT_TTL
          Field DEFAULT_TTL
 
Method Summary
static boolean pingNode(InetAddress addr)
          Transmit a single ICMP echo request packet to a node.
static long pingNode(InetAddress addr, byte ttl, byte[] response)
           Transmit a single ICMP echo request packet to a node.
static int pingNode(InetAddress addr, int count)
          Transmit multiple ICMP echo request packets to a node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_TTL

public static final byte DEFAULT_TTL
Field DEFAULT_TTL
Method Detail

pingNode

public static int pingNode(InetAddress addr,
                           int count)
Transmit multiple ICMP echo request packets to a node.
Parameters:
addr - ip address to be pinged
count - number of times to ping
Returns:
the number of packets received
See Also:
InetAddress

pingNode

public static boolean pingNode(InetAddress addr)
Transmit a single ICMP echo request packet to a node.
Parameters:
addr - ip address to be pinged
Returns:
true if a response is received
See Also:
InetAddress

pingNode

public static long pingNode(InetAddress addr,
                            byte ttl,
                            byte[] response)

Transmit a single ICMP echo request packet to a node.

This version of pingNode requires two additional parameters: the ttl byte specifying the time to live field in the IP datagram header of the oubound ICMP echo request and a byte array which is filled in with entire IP datagram received in response to the echo request. The method returns the time, measured in milliseconds, between transmitting the ICMP echo request message and receiving a response. If no response is received prior to the time-out period pingNode returns -1. The round trip time (RTT) estimate is measured in the native network stack and is therefore reasonably (within a few milliseconds) accurate. Any inaccuracy is on the high side and therefore the return value of pingNode provides an upper bound on the true round trip time.

As mentioned the response buffer receives the entire IP datagram containing the echo request response. Within the buffer the ICMP header and ICMP data are located after the IP header and can be located by calculating the IP header length as int ipHdrLength = (response[0] & 0x0f) << 2);. Using the IP header length as an offset the first field of the ICMP header, ICMP packet type, can be accessed as int type = response[ipHdrLength];

ICMP is specified in RFC 792

Parameters:
addr - ip address to be pinged
ttl - Time To Live (hop count) used in the IP header
response - buffer filled in with the repsonse, the length of the array should be >= 128
Returns:
the round trip time (between request and reply) in milliseconds, -1 if no response received
See Also:
InetAddress