gpt4 book ai didi

java - 在 Java 中按名称 Ping 计算机

转载 作者:搜寻专家 更新时间:2023-11-01 00:59:03 27 4
gpt4 key购买 nike

我正在编写一个简单的程序,从 MySQL 数据库中提取计算机名称,然后将这些名称存储到字符串数组列表中(这部分工作正常)。之后,我编写了一个类和一个方法,将字符串作为参数(这将是计算机名称)并尝试对其执行 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");
}
}

问题是,即使我可以手动对它们执行 ping 操作,或者如果我将计算机名称硬编码为“主机名”,我仍然会为大多数计算机抛出 UnknownHostException

这是我的主程序的样子:

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 的计算机进行试验。有人知道为什么会这样吗?

编辑...

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

pingComputer.pingHost("ROOM-1234");

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

编辑

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

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

感谢所有的建议!

最佳答案

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

使用下面的类来 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;
}
}

并使用如下代码

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 更好。

关于java - 在 Java 中按名称 Ping 计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12059932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com