6

I am in the process of writing a simple program that extracts computer names from MySQL Database then stores those names into a String array list (this part works fine). After that I wrote a class and a method that takes a String as a parameter (which will be the computer name) and tries to ping it. Here is the code for that class:

public class Ping 
{
public void pingHost(String hostName)
{   
    try
    {
        InetAddress inet = InetAddress.getByName(hostName);
        boolean status = inet.isReachable(5000);
        if (status)
        {
            System.out.println(inet.getHostName() + " Host Reached\t" + inet.getHostAddress());
        }
        else
        {
            System.out.println(inet.getHostName() + " Host Unreachable");
        }

    }
    catch (UnknownHostException e)
    {
        System.err.println(e.getMessage() + " Can't Reach Host");
    }
    catch (IOException e)
    {
        System.err.println(e.getMessage() + " Error in reaching the Host");
    }
}

The problem is that I keep getting UnknownHostException thrown for most computers even if I can ping them manually or if I hard code the computer name as the "hostName".

Here is what my main looks like:

public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException 
{
    ArrayList <String> list = new ArrayList<String>();  
    MySQLConnect myConnection = new MySQLConnect();
    myConnection.importData(list);
    Ping pingComputer = new Ping();
    pingComputer.pingHost(list.get(87));            
}

Right now I'm just trying to experiment with one computer which is throwing UnknownHostException but can be pinged manually. Anyone have any idea why this is happening?

EDIT...

Just to explain this a little bit more. For example in main, if I pass these parameters to pingHost:

pingComputer.pingHost("ROOM-1234");

It pings fine and returns correct host name/address. But list.get(87) returns same host name "ROOM-1234" but throws UnknownHostException? This has got me really confused and not sure why its not working.

EDIT

Wow finally figured it out. Reason ping was working when I was passing the string directly like so "ROOM-1234", was because there were no white spaces and getting is from array like so list.get(87) returned same thing but when I checked charLength, it returned a different value :) So I just ended up using trim to get rid of white spaces and now itworks great.

pingComputer.pingHost(list.get(87).trim());

Thanks for all the suggestions!

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Nick
  • 597
  • 3
  • 14
  • 34
  • 4
    How is the hostname passed? Is it fully-qualified-domain-name or just the name without domain information? – Vikdor Aug 21 '12 at 17:24
  • 1
    If you could just show how exactly do you use this method: pingHost. What do you pass there? – Ondrej Kvasnovsky Aug 21 '12 at 17:34
  • 2
    Please add some examples of actual hostnames that you pass to `pingHost()`. – Jim Garrison Aug 21 '12 at 17:38
  • 2
    This answer might give some insights: http://stackoverflow.com/questions/2448666/how-to-do-a-true-java-ping-from-windows – Denis Tulskiy Aug 21 '12 at 17:39
  • The hostname is determined by several ways in the network. To check if your solution works try adding the hostname to the hosts file (Windows: \windows\system32 or *nix: /etc/hosts). Address resolution is a complex issue because the subsystems involved. I suggest you to check some TCP/IP readings. – gersonZaragocin Aug 21 '12 at 17:46
  • Host name looks like this (ex): ROOM-1234, ROOM-1235... Manually I can ping them, but when I pass them to a method as shown in my main pingComputer.pingHost(list.get(87)) it returns UnknownHostException. list.get(87) returns a String which is the computer name as indicated above. – Nick Aug 21 '12 at 17:51

1 Answers1

-2

Dear Actually the code you are using is to check whether the host is reachable or not.

Use following class to ping windows pc use ping method but for other than windows pc use isreachable.

package com.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Ping {

    public Boolean IsReachable(String ipaddress) {
        try {            
            final InetAddress host = InetAddress.getByName(ipaddress);

            try {
                return host.isReachable(3000);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Boolean ping(String ipaddress) {
        Runtime runtime = Runtime.getRuntime();
        String cmds = "ping " + ipaddress;
        System.out.println(cmds);
        Process proc;

        try {
            proc = runtime.exec(cmds);
            proc.getOutputStream().close();
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            String line;

            while ((line = bufferedreader.readLine()) != null) {
                if (line.contains("Reply from " + ipaddress + ":")) {
                    return true;
                }
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        return false;
    }
}

And use as below code

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    MySQLConnect myConnection = new MySQLConnect();
    myConnection.importData(list);
    Ping ping = new Ping();

    if (ping.ping(list.get(87)) {
        System.out.prinln("Online / Host is reachable");
    } else {
        System.out.prinln("Offline /Host is unreachable");
    }
}

But I would suggest ping by ip address is better than pinging with computer name.

prem30488
  • 2,828
  • 2
  • 25
  • 57
  • 1
    This is not a good general solution. (1) The ping command may not exist. (2) The ping output may not be in that format - OS X will say x bytes from, and on non-English versions of Windows the messages will be translated into the local language. – nobody Apr 06 '14 at 18:51