简体   繁体   English

在Java中获取连接到LAN的所有主机的IP地址

[英]geting ip address of all host connected to LAN in java

I want to develop a LAN search tool. 我想开发一个局域网搜索工具。 For that I need all connected host IPs. 为此,我需要所有连接的主机IP。 For that I'm using this, but it did not work for me. 为此,我正在使用它,但是它对我不起作用。 It gives all result not reachable, even my own IP. 它给出了所有结果,甚至我自己的IP,都无法达到。

Please help me whats going wrong in this 请帮我解决这个问题

public static void checkHosts(String subnet) throws UnknownHostException, IOException{
  int timeout=1000;
   for (int i=1;i<254;i++){
       System.out.println(i);
   String host=subnet + "." + i;
   if (InetAddress.getByName(host).isReachable(timeout)){
       System.out.println(host + " is reachable");
   }
   else{
       System.out.println(host + " is not reachable");
   }
}

Your code seems fine, try increasing the timeout . 您的代码看起来不错,请尝试增加超时时间

4s-5s timeout would be great, it should at least show your own IP as reachable. 4s-5s超时会很棒,它至少应该显示您自己的IP可达。


Also, isReachable 's documentation says: 另外, isReachable的文档说:

Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status. 该实现会尽最大努力尝试到达主机,但是防火墙和服务器配置可能会阻止请求,从而导致无法访问状态。

So, if other cases fail make sure there are no firewall issues. 因此,如果其他情况失败,请确保没有防火墙问题。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM