J++ using ping command

I'm trying to write a windows app that uses the dos ping command. Simply help, how do you do go about reading/writing text write "ping 192.234.72.5" and then read the response. Any pointers in the right direction would be great.
GrahamOrrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JodCommented:
An example from Inquirt.com...

This program opens a connection to the echo server (always port 7) of a given host (using the domain name instead of the IP address, hope that's okay), sends the string "Hello," reads the response, then compares it with the string sent:

import java.io.*;
import java.net.*;

class Ping {

   public static void main(String args[]) {
      try {
         Socket t = new Socket(args[0], 7);
         DataInputStream is = new DataInputStream(t.getInputStream());
         PrintStream os = new PrintStream(t.getOutputStream());

         os.println("Hello");
         String str = is.readLine();

         if (str.equals("Hello"))
           System.out.println("Alive :-)") ;
         else
            System.out.println("Dead :-(");

         t.close();
      }

      catch (IOException e) {System.out.println("Error: " + e);}
   }
}

run it like...

java Ping [host name]


You could also use the exec command to execute the Ping command from a dos box.

Do you need to know how to do this? Well you can do it like this...

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
/**
 * Workaround for jdk 1.1 bug in NT where Process.waitFor()
 * never comes back.  This has only been tested on NTW 4.0.
 */
public class SystemCall {
  public static void main( String args[]) {
  try {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec( "ping 192.234.72.5");
    InputStream  in  = p.getInputStream();
    boolean finished = false;  // Set to true when p is finished
    while( !finished) {
      try {
        while( in.available() > 0) {
          // Print the output of our system call.
          Character c = new Character( (char) in.read());
          System.out.print( c);
        }
        // Ask the process for its exitValue.  If the process
        // is not finished, an IllegalThreadStateException
        // is thrown.  If it is finished, we fall through and
        // the variable finished is set to true.
        p.exitValue();
        finished = true;
      } catch (IllegalThreadStateException e) {

    // Sleep a little to save on CPU cycles
    Thread.currentThread().sleep(500);

    }
  }
} catch (Exception e) { System.err.println( e); }
}
}

This fires off the ping command in a dos shell and captures the output from it so you can print it our or examine it.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JodCommented:
Cheers, GrahamOrr.

Only a B though? Must have made a spelling mistake...:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.