InetAddress类中的isReachable问题 [英] Problem with isReachable in InetAddress class

查看:829
本文介绍了InetAddress类中的isReachable问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一项任务,我必须找到局域网上所有活着的计算机。我正在使用 isReachable 函数 InetAddress 类。但问题是没有任何东西可以显示给我。所以我尝试使用Google的IP isReachable ,但仍然无法访问。

As an assignment I have to find all the alive computers on a LAN. For which I am using isReachable function of InetAddress class. But problem is that nothing is shown reachable to me. So I tried to have isReachable with Google's IP but still this is unreachable.

以下是代码:

import java.net.*;

public class alive{
    public static void main(String args[]){
        try{
            InetAddress ia = InetAddress.getByAddress(new byte[]{(byte)209, (byte)85, (byte)153, (byte)104});
            boolean b = ia.isReachable(10000);
            if(b){
                System.out.println("Reachable");
            }
            else{
                System.out.println("Unrachable");
            }

        }catch(Exception e){
            System.out.println("Exception: " + e.getMessage());
        }
    }
}

输出为:无法访问

推荐答案

以下是有关为什么isReachable()可能无法始终工作的详细信息预期

Here are some details on why isReachable() might not always work as expected


  1. http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html

  2. http://www.coderanch.com/t/206934/sockets/java/InetAdress-isReachable -Ping-Permissions

  1. http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html
  2. http://www.coderanch.com/t/206934/sockets/java/InetAdress-isReachable-Ping-Permissions

正确的方法是使用 ICMP 协议。我相信这就是 ping 使用internatlly。 以下是一个示例开始了。

The correct way for you is to use the ICMP protocol. This is what ping uses internatlly, I believe. Here is an example that get you started.

这篇关于InetAddress类中的isReachable问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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