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

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" and then read the response. Any pointers in the right direction would be great.
  • 2
1 Solution
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());

         String str = is.readLine();

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


      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");
    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.
        finished = true;
      } catch (IllegalThreadStateException e) {

    // Sleep a little to save on CPU cycles

} 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.

Cheers, GrahamOrr.

Only a B though? Must have made a spelling mistake...:)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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