Solved

ICMP/PING

Posted on 1997-05-21
1
1,746 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

726 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