• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

PING functionality in Java

Is it possible to implement the ping functionality in Java?
i.e. We would have an input box in a html page, where a user types the IP address or the server name ...
The result page should display either timed out or success like the way the ping utility in the windows work.
0
hariharann
Asked:
hariharann
  • 5
  • 3
  • 2
  • +4
1 Solution
 
glottisCommented:
of course it is possible.

you would have to work with JSP or servlets or other language. The server which will be PINGed can be implemented using Sockets.
0
 
objectsCommented:
It's not possible using pure Java.
Ping uses ICMP protocol. Java only supports UDP/TCP.

Here's a Windows implementation:
http://www.geocities.com/SiliconValley/Bit/5716/ping/index_eng.html
0
 
girionisCommented:
 You could always use the Runtime.getRutime().exec() method to ping a computer and read the response back.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TimYatesCommented:
Or do something like this:  (assuming you just want the basic functionality)

/*
 *  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;
        {}
    }
}

------------
http://www.jadcentral.com/newscentral/feature.jsp?feature_ID=49
0
 
danBoshCommented:
there isn't a pure way of achieveing ping in java. I achieved the same effect by having a method on the client side that does nothing, then on the server side i call this and see how long it takes to execute. I do this three times and then take an average.
0
 
objectsCommented:
Tim,

Thats not really an implementation of ping, it simply tests if port 7 is open.
0
 
TimYatesCommented:
I know...but ping 9 times out of 10 is used to check if a machine is alive...

I guess if you changed it to check Port 7, then if that fails the other regular ports for *NIX and Windows

It won't do timing tho
0
 
sciuriwareCommented:
I agree with Tim: it could be a 2 stage check,
if 7 fails there isn't even TCP available.
The next stage could check for telnet or ssh.
;JOOP!
0
 
TimYatesCommented:
Yup :-)

And if you NEED timing, then either girionis or danBosh's approach is the best (and oly) way of doing it...
0
 
objectsCommented:
> I guess if you changed it to check Port 7, then if that
> fails the other regular ports for *NIX and Windows

Point is though the question asked if ping protocol could be implemented in Java, not how to check if a machine is alive :)


0
 
objectsCommented:
Or maybe we are just interpreting the question differently.
Any hariharann doesn't seem interested in the discussion anyway. I should have checked his/her profile before answering:

Questions Asked 44
Last 10 Grades Given B B B B B A B B B A  
Question Grading Record 27 Answers Graded / 27 Answers Received
0
 
hariharannAuthor Commented:
Hello everyone,
Sorry for not responding as I was away on leave. Thanks all for the reply. We have decided to use the Runtime.exec() method to call an external utility to get the job done.
0
 
girionisCommented:
 :-)
0
 
danBoshCommented:
whats wrong with hariharann profile? is it fround upon it give Bs?
0
 
objectsCommented:
17 ungraded questions.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now