如何使用Java获取在同一网络(子网)中连接的IP列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3345857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:29:12  来源:igfitidea点击:

How to get a list of IP connected in same network (subnet) using Java

javanetworking

提问by Jigar Joshi

How do I get list of IP addresses for devices connected to my same subnet using Java?

如何使用 Java 获取连接到同一子网的设备的 IP 地址列表?

采纳答案by fasseg

this should work when the hosts on your network react to ICMP packages (ping) (>JDK 5):

当您网络上的主机对 ICMP 包 (ping) (>JDK 5) 做出反应时,这应该可以工作:

public void checkHosts(String subnet){
   int timeout=1000;
   for (int i=1;i<255;i++){
       String host=subnet + "." + i;
       if (InetAddress.getByName(host).isReachable(timeout)){
           System.out.println(host + " is reachable");
       }
   }
}

invoke the method for a subnet (192.168.0.1-254) like this:

像这样调用子网 (192.168.0.1-254) 的方法:

checkHosts("192.168.0");

didnt test it but should work kinda like this. Obviously this only checks the 254 hosts in the last byte of the ip address...

没有测试它,但应该像这样工作。显然这只检查ip地址最后一个字节中的254台主机......

check:

查看:

http://download-llnw.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

http://download-llnw.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29 http://blog.taragana.com/index.php/archive/how -to-do-icmp-ping-in-java-jdk-15-and-above/

hope that helped

希望有所帮助

回答by Suhas Phartale

To list the hosts connected in a LAN you will need to ping all the available IP addresses on the subnet. But a ping message could be restricted by firewall thus safer way could be open a socket to each IP address in the LAN's IP address range.

要列出 LAN 中连接的主机,您需要 ping 子网上的所有可用 IP 地址。但是 ping 消息可能会受到防火墙的限制,因此更安全的方法是为 LAN 的 IP 地址范围内的每个 IP 地址打开一个套接字。

回答by Scott

If you mean a list of all hosts connected to the network, I think the only way that is guaranteed to work is to step through a list of IP addresses and ping them all.

如果您的意思是连接到网络的所有主机的列表,我认为保证工作的唯一方法是单步执行 IP 地址列表并将它们全部 ping 通。

That said, if you're looking for something more specific, there may be something you can look up (e.g. RMI's registry (LocateRegistry.getRegistry(host, port).list()).

也就是说,如果您正在寻找更具体的东西,您可以查找一些东西(例如 RMI 的注册表 ( LocateRegistry.getRegistry(host, port).list())。

Also, if you just want all the IP addresses that a given host has, have a look at NetworkInterface.getNetworkInterfaces().

此外,如果您只想要给定主机拥有的所有 IP 地址,请查看NetworkInterface.getNetworkInterfaces().

回答by vicatcu

Since Java 1.5 there is a ping-like method in java.net.InetAddress: public boolean isReachable(int timeout). You could use that to iterate over all the IP Addresses in your subnet... java-doc

从 Java 1.5 开始,java.net.InetAddress 中有一个类似 ping 的方法:public boolean isReachable(int timeout)。您可以使用它来遍历子网中的所有 IP 地址... java-doc

回答by user207421

One of the problems here is that neither of the terms "LAN" and "connected" has a meaning in TCP/IP. The suggested technique of calling isReachable() on all the hosts in the class D subnet might work if your LAN corresponds precisely to a class-D subnet.

这里的问题之一是“LAN”和“已连接”这两个术语在 TCP/IP 中都没有意义。如果您的 LAN 恰好对应于 D 类子网,则建议的在 D 类子网中的所有主机上调用 isReachable() 的技术可能会起作用。

You might be better off looking at SAMBA, which can interrogate the LAN members via SMBs, so at least you'll be using a technique that has the same meaning for LAN that you do.

您最好看看 SAMBA,它可以通过 SMB 询问 LAN 成员,因此至少您将使用与 LAN 具有相同含义的技术。

回答by Thusitha Wickramasinghe

Shows Active Addresses On LAN

显示 LAN 上的活动地址

public static void main(String[] args) {
    try {
        Enumeration nis = NetworkInterface.getNetworkInterfaces();
        while(nis.hasMoreElements())
        {
            NetworkInterface ni = (NetworkInterface) nis.nextElement();
            Enumeration ias = ni.getInetAddresses();
            while (ias.hasMoreElements())
            {
                InetAddress ia = (InetAddress) ias.nextElement();
                System.out.println(ia.getHostAddress());
            }

        }
    } catch (SocketException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Output

输出

127.0.0.1
0:0:0:0:0:0:0:1
172.128.1.102

回答by Sumukha Pk

None of these were working for me as I had created the server and it didn't respond to these pings. So I created a mechanism that makes the server reply to a ping and used the brute force checking method for all addresses in the subnet.

这些都不适合我,因为我创建了服务器并且它没有响应这些 ping。因此,我创建了一种机制,使服务器回复 ping,并对子网中的所有地址使用暴力检查方法。

PS: Writing this because someone creating their own server might need this.

PS:写这个是因为有人创建自己的服务器可能需要这个。