我正在使用这部分代码来ping java中的ip地址,但只有ping localhost成功,而对于其他主机,程序说主机无法访问.我禁用了防火墙,但仍然遇到此问题
public static void main(String[] args) throws UnknownHostException, IOException {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
ipAddress = "173.194.32.38";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
发送Ping请求到127.0.0.1
主机可达
发送Ping请求到173.194.32.38无法
访问主机
小智 30
我认为此代码可以帮助您:
public class PingExample {
public static void main(String[] args){
try{
InetAddress address = InetAddress.getByName("192.168.1.103");
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dur*_*dal 18
检查您的连接.在我的计算机上,这两个IP打印REACHABLE:
发送Ping请求到127.0.0.1
主机可达
发送Ping请求到173.194.32.38
主机可达
编辑:
您可以尝试修改代码以使用getByAddress()来获取地址:
public static void main(String[] args) throws UnknownHostException, IOException {
InetAddress inet;
inet = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
inet = InetAddress.getByAddress(new byte[] { (byte) 173, (byte) 194, 32, 38 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
Run Code Online (Sandbox Code Playgroud)
getByName()方法可能会尝试某种反向DNS查找,这在您的机器上可能是不可能的,getByAddress()可能会绕过它.
are*_*tai 15
您不能简单地使用Java进行ping操作,因为它依赖于ICMP,这在Java中是不可支持的
http://mindprod.com/jgloss/ping.html
请改用套接字
希望能帮助到你
gYa*_*anI 12
它肯定会起作用
import java.io.*;
import java.util.*;
public class JavaPingExampleProgram
{
public static void main(String args[])
throws IOException
{
// create the ping command as a list of strings
JavaPingExampleProgram ping = new JavaPingExampleProgram();
List<String> commands = new ArrayList<String>();
commands.add("ping");
commands.add("-c");
commands.add("5");
commands.add("74.125.236.73");
ping.doCommand(commands);
}
public void doCommand(List<String> command)
throws IOException
{
String s = null;
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此方法在Windows或其他平台上ping主机:
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
int returnVal = proc.waitFor();
return returnVal == 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
简短的建议:不要使用 isReachable(),调用系统 ping,如上面一些答案中所建议的那样。
长解释:
| 归档时间: |
|
| 查看次数: |
228618 次 |
| 最近记录: |