在 Java 中按名称 Ping 计算机 [英] Pinging computer by name in Java

查看:64
本文介绍了在 Java 中按名称 Ping 计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");
    }
}

问题是,对于大多数计算机,即使我可以手动 ping 它们或者将计算机名称硬编码为hostName",我也会不断抛出 UnknownHostException.

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

这是我的主要内容:

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));            
}

现在我只是想用一台抛出 UnknownHostException 但可以手动 ping 的计算机进行试验.有人知道为什么会这样吗?

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?

编辑...

只是稍微解释一下.例如在 main 中,如果我将这些参数传递给 pingHost:

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

pingComputer.pingHost("ROOM-1234");

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

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.

编辑

哇终于明白了.当我像ROOM-1234"那样直接传递字符串时,ping 起作用的原因是因为没有空格并且从数组中获取,就像这样 list.get(87) 返回相同的内容,但是当我检查 charLength 时,它返回了一个不同的值 :) 所以我最终使用 trim 来去除空格,现在它很好用.

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());

感谢所有建议!

推荐答案

Dear 其实你用的代码就是检查主机是否可达.

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

使用下面的类来ping windows pc 使用ping 方法但是对于windows pc 以外的使用是可以访问的.

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

并使用如下代码

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");
    }
}

但我建议通过 ip 地址 ping 比使用计算机名称 ping 更好.

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

这篇关于在 Java 中按名称 Ping 计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆