?
Solved

PING functionality in Java

Posted on 2003-03-31
15
Medium Priority
?
527 Views
Last Modified: 2009-12-16
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
Comment
Question by:hariharann
[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
  • 5
  • 3
  • 2
  • +4
15 Comments
 
LVL 2

Expert Comment

by:glottis
ID: 8243189
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
 
LVL 92

Expert Comment

by:objects
ID: 8243440
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
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 8244309
 You could always use the Runtime.getRutime().exec() method to ping a computer and read the response back.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:TimYates
ID: 8244444
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
 

Expert Comment

by:danBosh
ID: 8244606
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
 
LVL 92

Expert Comment

by:objects
ID: 8249727
Tim,

Thats not really an implementation of ping, it simply tests if port 7 is open.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8252708
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 8252736
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8252775
Yup :-)

And if you NEED timing, then either girionis or danBosh's approach is the best (and oly) way of doing it...
0
 
LVL 92

Expert Comment

by:objects
ID: 8257132
> 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
 
LVL 92

Expert Comment

by:objects
ID: 8257147
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
 

Author Comment

by:hariharann
ID: 8258905
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
 
LVL 35

Expert Comment

by:girionis
ID: 8259631
 :-)
0
 

Expert Comment

by:danBosh
ID: 8259951
whats wrong with hariharann profile? is it fround upon it give Bs?
0
 
LVL 92

Expert Comment

by:objects
ID: 8259969
17 ungraded questions.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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