java 查找本地网络中的所有IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32500182/
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
Find all IP addresses in local network
提问by BullyWiiPlaza
I want to find all IP addresses of devices in the local network I'm currently connected to using Java code. The useful utility Advanced IP Scanner
is able to find various IP addresses in my subnetof 192.168.178/24
:
我想在当前使用 Java 代码连接到的本地网络中查找设备的所有 IP 地址。有用的实用程序Advanced IP Scanner
能够在我的子网中找到各种 IP 地址192.168.178/24
:
According to thisanswer, I built my code the following way:
根据这个答案,我通过以下方式构建了我的代码:
import java.io.IOException;
import java.net.InetAddress;
public class IPScanner
{
public static void checkHosts(String subnet) throws IOException
{
int timeout = 100;
for (int i = 1; i < 255; i++)
{
String host = subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout))
{
System.out.println(host + " is reachable");
}
}
}
public static void main(String[] arguments) throws IOException
{
checkHosts("192.168.178");
}
}
Unfortunately, this does not print out any results, meaning that no IP addresses are reachable. Why? There are devices in my local network like seen in the Advanced IP Scanner
scan.
不幸的是,这不会打印出任何结果,这意味着无法访问任何 IP 地址。为什么?我的本地网络中有设备,如Advanced IP Scanner
扫描中所见。
回答by The_Programmer
Try to increase the timeout. I used about 5000ms, this helped me. In case you don't want to wait 5000ms * 254 = 21 minutes, try also this code with parallel pinging to the addresses:
尝试增加超时。我使用了大约 5000 毫秒,这对我有帮助。如果您不想等待 5000 毫秒 * 254 = 21 分钟,也可以尝试使用并行 ping 地址的代码:
public static void getNetworkIPs() {
final byte[] ip;
try {
ip = InetAddress.getLocalHost().getAddress();
} catch (Exception e) {
return; // exit method, otherwise "ip might not have been initialized"
}
for(int i=1;i<=254;i++) {
final int j = i; // i as non-final variable cannot be referenced from inner class
new Thread(new Runnable() { // new thread for parallel execution
public void run() {
try {
ip[3] = (byte)j;
InetAddress address = InetAddress.getByAddress(ip);
String output = address.toString().substring(1);
if (address.isReachable(5000)) {
System.out.println(output + " is on the network");
} else {
System.out.println("Not Reachable: "+output);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start(); // dont forget to start the thread
}
}
Worked perfectly for me.
非常适合我。
回答by seneque
InetAddress.isReachable will use ICMP ECHO REQUEST (as when you do a ping) or request on port 7 (echo port): http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable%28int%29
InetAddress.isReachable 将使用 ICMP ECHO REQUEST(当您执行 ping 时)或端口 7(回显端口)上的请求:http: //docs.oracle.com/javase/7/docs/api/java/net/InetAddress。 html#isReachable%28int%29
Advance IP scanner perhaps use an other way to discover the hosts (like a request on radmin port or a request on http).
高级 IP 扫描器可能会使用其他方式来发现主机(例如对 radmin 端口的请求或对 http 的请求)。
An host can be up but not answering to ICMP ECHO REQUEST.
主机可以启动但不响应 ICMP ECHO REQUEST。
have you try to ping one of the host from command line?
您是否尝试从命令行 ping 一台主机?
回答by vgm
Maybe try using InetAddress.getByAddress(host)
instead of getByName
, like this:
也许尝试使用InetAddress.getByAddress(host)
而不是getByName
,像这样:
InetAddress localhost = InetAddress.getLocalHost();
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
try
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(100))
{
output = address.toString().substring(1);
System.out.print(output + " is on the network");
}
}
I took this sample for autodetection code from here
我从这里拿了这个样本用于自动检测代码
回答by Ivo Skalicky
Java 8 stream solution
Java 8 流解决方案
IntStream.rangeClosed(1,254).mapToObj(num -> "192.168.0."+num).parallel()
.filter((addr) -> {
try {
return InetAddress.getByName(addr).isReachable(2000);
} catch (IOException e) {
return false;
}
}
).forEach(System.out::println);