#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    31
    Rep Power
    9

    Question Help needed in writing a ping client ?


    Hi, I have a requirement to write some java code that allows me to ping and IP address (or hostname) to determine if the host is responding. I have been trying to get something to work using commons-net echo class but with limited success, the code i have been using is shown below:

    import java.io.*;
    import java.net.*;
    import org.apache.commons.net.*;

    public class commonsping
    {
    public static void main(String args[]) throws IOException
    {
    EchoTCPClient client = new EchoTCPClient();
    BufferedReader input, echoInput;
    PrintWriter echoOutput;
    String line;
    String host="192.168.1.3";

    // We want to timeout if a response takes longer than 60 seconds
    client.setDefaultTimeout(60000);
    client.connect(host);
    System.out.println("Connected to " + host + ".");
    input = new BufferedReader(new InputStreamReader(System.in));
    echoOutput =
    new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
    echoInput =
    new BufferedReader(new InputStreamReader(client.getInputStream()));

    while ((line = input.readLine()) != null)
    {
    echoOutput.println(line);
    System.out.println(echoInput.readLine());
    }

    client.disconnect();
    }
    }

    However when i run the code i get a connection refused error message (i have disabled my firewall but still get the same error)

    So as an alternative I tried to write something using the InetAddress.getByName function, at first this seemed to function well but after further testing i discovered for whatever reason it was unable to reach certain valid IP addresses and also if the timeout was extended to say 15 secs would report that an IP address that doesnt even exist on the network is reachable!


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

    public class ping {
    public static void main(String args[]) {
    try {

    InetAddress
    address=InetAddress.getByName("192.168.1.1");
    System.out.println("Name: " + address.getHostName());
    System.out.println("Addr: " + address.getHostAddress());
    System.out.println("Reach: " + address.isReachable(3000));
    }
    catch (UnknownHostException e) {
    System.err.println("Unable to lookup host");
    }
    catch (IOException e) {
    System.err.println("Unable to reach host");
    }
    }
    }


    I would really appreciate any help on this or if someone could just point me in the right direction.

    Many thanks

    Danny
  2. #2
  3. No Profile Picture
    Needs a pillow.... :)
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2006
    Posts
    681
    Rep Power
    91
    Why not use the exec() method?
  4. #3
  5. No Profile Picture
    Needs a pillow.... :)
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2006
    Posts
    681
    Rep Power
    91
    Just an example...
    java Code:
    public boolean alive(String address){
      boolean returnValue;
      Process pro = Runtime.getRuntime().exec("ping "+address);
      int result = pro.waitFor();
      if(result == 0){
        returnValue = true;
      }else{
        retrunValue = false;
      }
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    31
    Rep Power
    9

    Question


    Originally Posted by jtalerico
    Just an example...
    java Code:
    public boolean alive(String address){
      boolean returnValue;
      Process pro = Runtime.getRuntime().exec("ping "+address);
      int result = pro.waitFor();
      if(result == 0){
        returnValue = true;
      }else{
        retrunValue = false;
      }
    }
    Thanks for the reply, I am trying to get this code to run but am getting the following error:

    ping.java:19: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
    int result = pro.waitFor();

    What Exception must i declare, i have already declared a throws IOException ?
  8. #5
  9. No Profile Picture
    Needs a pillow.... :)
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2006
    Posts
    681
    Rep Power
    91
    java Code:
    public boolean alive(String address){
            boolean returnValue;
            try{
            Process pro = Runtime.getRuntime().exec("ping "+address);
            int result = pro.waitFor();
            if(result == 0){
              returnValue = true;
            }else{
              retrunValue = false;
            }
          } 
    }Catch(Exception e){
     System.out.println("Unreconized command \n" + e);
    }


    do a try catch block... Sorry missed that.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    31
    Rep Power
    9

    Smile


    Originally Posted by jtalerico
    java Code:
    public boolean alive(String address){
            boolean returnValue;
            try{
            Process pro = Runtime.getRuntime().exec("ping "+address);
            int result = pro.waitFor();
            if(result == 0){
              returnValue = true;
            }else{
              retrunValue = false;
            }
          } 
    }Catch(Exception e){
     System.out.println("Unreconized command \n" + e);
    }


    do a try catch block... Sorry missed that.
    Many thanks that works a treat an I much appreciate you help.

    Regards

    Danny
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Posts
    1
    Rep Power
    0
    I have similar problem too, and this code help me, but please say how I can get more information from system output?
    I need to know precisely system output on ping command, and not only ping. I think I should use getOutputStream or getInputStream, but I don't know how to print it..
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Unhappy Ping Code Issue


    Originally Posted by jtalerico
    Just an example...
    java Code:
    public boolean alive(String address){
      boolean returnValue;
      Process pro = Runtime.getRuntime().exec("ping "+address);
      int result = pro.waitFor();
      if(result == 0){
        returnValue = true;
      }else{
        retrunValue = false;
      }
    }
    This code works great however, when I run my application and the IP address is invalid it will not time out. It just hangs on the android device.

    I tried putting -w 2 and -W 3 but still it hangs up and I have to kill it.

    Also, I installed a command prompt on the device and ran the ping command at the Unix prompt with the -w option and it works fine. Why does this code hang up and not time out as expected? Is there something I can do to fix this?

    Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo