Solved

ICMP/PING

Posted on 1997-05-21
1
1,740 Views
Last Modified: 2012-06-21
How can I program a PING command under Java. I don't want to  create daemons on each host but simply use their TCP/IP stack to respond to a ping. Others ideas would be welcome.
0
Comment
Question by:mido
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 200 total points
Comment Utility
Java does not support Raw packets, only UDP (SOCK_DGRAM)  and TCP (SOCK_STREAM), so ICMP is not possible

Additionaly, several platforms (including Win95 and NT) implementation of the TCP/IP don't even support raw sockets.Add to that that many UNIX hosts have disabled Ping do to ICMP attacks and the situation looks bad.

Fortunatley there is a work arround attributed to Qusay H. Mahmoud in the March issue of Java Cats (http://www.javacats.com/US/)

Its based on sending a TCP or UDP packet to the hosts echo port (port 7). If the connection cannot be establishd it is considered unreachable, otherwise its considered found. (It has the added advantage that it doesn't even have to send data).

The folling is Mahmoud's Java Ping class:


/*
 *  Program: ping.java
 *  @author: Qusay H. Mahmoud
 * @version: Nov 2, 1996
 */

import java.io.*;
import java.net.*;

public class ping {
    public final static int ECHO_PORT = 7;
    public static void main(String argv[]) {
        if (argv.length != 1) {
          System.out.println("Usage: java ping hostname");
          System.exit(0);
        }
        if (alive(argv[0])) {
          System.out.println(argv[0] + " is alive");
        } else {
         System.out.println("No response from " + argv[0] +". Host is down or does not exist");
        }
    }
 
    public static boolean alive(String host) {
        Socket pingSocket = null;
        try {
            pingSocket = new Socket(host, ECHO_PORT);
        } catch (UnknownHostException e) {
            System.err.println("UnknownHostException: " +e);
        } catch (IOException io) {
            System.out.println("IOException: " + io);
        }
        if (pingSocket != null) {
           try {
              pingSocket.close();
           } catch (IOException e) {
                System.err.println("IOException: " + e);
           }
           return true;
        } else {
           return false;
        }
    }
}

Hope this solves your problem.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now