Java的InetAddress.getByName方法返回主机名/ IP,但会导致ping失败。

huangapple go评论63阅读模式
英文:

Java InetAddress.getByName returns hostname/ip but it causes ping to fail

问题

我正在使用Corretto Java 11 JDK。

以下是我的方法:

public static boolean sendPingRequest(String host)
{
    boolean value = false;

    try
    {
        InetAddress myip = InetAddress.getByName(host);
        System.out.println("发送Ping请求到:" + myip);

        if (myip.isReachable(5000))
        {

            System.out.println("主机:" + host + " 可达");
            value = true;
        }
        else
        {

            System.out.println("主机:" + host + " 不可达");
        }
    }
    catch (UnknownHostException e)
    {

        System.out.println("无法访问此主机:" + e.getMessage());
        e.printStackTrace();
    }
    catch (IOException e)
    {
        System.out.println("IO异常:" + e.getMessage());
    }
    return value;
}

当我在这里调用该方法时,它失败了:

String myHost = "www.google.com";
sendPingRequest(myHost);

发送Ping请求到www.google.com/108.177.111.106
主机:www.google.com 不可达
进程以退出代码0结束

从阅读文档中看来,InetAddress.getByName方法返回这个,但除非我能让它显示主机为IP 108.177.111.106,否则它每次都会失败。

英文:

I am using correto java 11 JDK

Here is my method:

public static boolean sendPingRequest(String host)
{
    boolean value = false;

    try
    {
        InetAddress myip = InetAddress.getByName(host);
        System.out.println("Sending Ping Request to " + myip);

        if (myip.isReachable(5000))
        {

            System.out.println("Host: " + host + " is reachable");
            value = true;
        }
        else
        {

            System.out.println("Host: " + host + " is unreachable");
        }
    }
    catch (UnknownHostException e)
    {

        System.out.println("I can't reach this host: " + e.getMessage());
        e.printStackTrace();
    }
    catch (IOException e)
    {
        System.out.println("IO Exception" + e.getMessage());
    }
    return value;
}

When I can the method here it fails

String myHost = "www.google.com";
sendPingRequest(myHost);

Sending Ping Request to www.google.com/108.177.111.106
Host: www.google.com is unreachable
Process finished with exit code 0

From reading the documentation it looks like InetAddress.getByName method returns this but it will fail every time unless I can get it to show the host to be the IP 108.177.111.106.

答案1

得分: 1

我无法重现您的问题。这是我尝试过的:

public class Main {

    public static void main(String[] args) {
        System.out.println(sendPingRequest("www.google.com"));
        System.out.println(sendPingRequest("74.125.195.106"));
        System.out.println(sendPingRequest("108.177.111.106"));
    }

    public static boolean sendPingRequest(String host) {
        boolean value = false;

        try {
            InetAddress myip = InetAddress.getByName(host);
            System.out.println("正在发送 Ping 请求至 " + myip);
            if (myip.isReachable(5000)) {
                System.out.println("主机:" + host + " 是可达的");
                value = true;
            } else {
                System.out.println("主机:" + host + " 是不可达的");
            }
        } catch (UnknownHostException e) {
            System.out.println("我无法访问此主机:" + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IO 异常:" + e.getMessage());
        }
        return value;
    }
}

输出为:

正在发送 Ping 请求至 www.google.com/74.125.195.105
主机www.google.com 是可达的
true
正在发送 Ping 请求至 /74.125.195.106
主机74.125.195.106 是可达的
true
正在发送 Ping 请求至 /108.177.111.106
主机108.177.111.106 是可达的
true

也许您当时遇到了网络问题?

英文:

I can't reproduce your issue. Here's what I tried:

public class Main {

    public static void main(String[] args) {
        System.out.println(sendPingRequest("www.google.com"));
        System.out.println(sendPingRequest("74.125.195.106"));
        System.out.println(sendPingRequest("108.177.111.106"));
    }

    public static boolean sendPingRequest(String host) {
        boolean value = false;

        try {
            InetAddress myip = InetAddress.getByName(host);
            System.out.println("Sending Ping Request to " + myip);
            if (myip.isReachable(5000)) {
                System.out.println("Host: " + host + " is reachable");
                value = true;
            } else {
                System.out.println("Host: " + host + " is unreachable");
            }
        } catch (UnknownHostException e) {
            System.out.println("I can't reach this host: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IO Exception" + e.getMessage());
        }
        return value;
    }
}

and the output is

Sending Ping Request to www.google.com/74.125.195.105
Host: www.google.com is reachable
true
Sending Ping Request to /74.125.195.106
Host: 74.125.195.106 is reachable
true
Sending Ping Request to /108.177.111.106
Host: 108.177.111.106 is reachable
true

Perhaps you were having network issues?

huangapple
  • 本文由 发表于 2020年10月17日 03:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64395601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定