Solved

ICMP/PING

Posted on 1997-05-21
1
1,747 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 200 total points
ID: 1220691
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

717 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