Solved

ICMP/PING

Posted on 1997-05-21
1
1,745 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
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
Running JavaFX on JDeveloper 12C 1 53
hashmap order 17 36
Java Eclipse Loop 3 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 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