在Java中按名称ping计算机

Nic*_*ick 5 java ping

我正在编写一个简单的程序,该程序将从MySQL数据库中提取计算机名称,然后将这些名称存储到String数组列表中(这部分工作正常)。之后,我编写了一个类和一个方法,该方法将String作为参数(将是计算机名称),并尝试对其执行ping操作。这是该类的代码:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,UnknownHostException即使我可以手动ping多数计算机,或者将计算机名称硬编码为“ hostName” ,我仍然会被大多数计算机抛出。

这是我的主要样子:

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));            
}
Run Code Online (Sandbox Code Playgroud)

现在,我只是尝试试验一台正在抛出UnknownHostException但可以手动ping通的计算机。有人知道为什么会这样吗?

编辑...

只是为了解释这一点。例如在main中,如果我将这些参数传递给pingHost

pingComputer.pingHost("ROOM-1234");
Run Code Online (Sandbox Code Playgroud)

可以ping通并返回正确的主机名/地址。但是list.get(87)返回相同的主机名“ ROOM-1234”却抛出UnknownHostException?这让我很困惑,不确定为什么它不起作用。

编辑

哇终于明白了。当我像“ ROOM-1234”这样直接传递字符串时,ping正常工作的原因是因为没有空格,并且从数组中获取是这样的,所以list.get(87)返回相同的东西,但是当我检查时charLength,它返回了一个不同的值:)所以我刚trim用来消除空白,现在效果很好。

pingComputer.pingHost(list.get(87).trim());
Run Code Online (Sandbox Code Playgroud)

感谢所有的建议!

Mr.*_*. P -2

亲爱的,实际上您使用的代码是检查主机是否可达。

使用以下类来 ping Windows PC 使用 ping 方法,但对于 Windows 以外的 PC 使用 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用如下代码

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

但我建议通过 IP 地址 ping 比通过计算机名称 ping 更好。