Java InetAddress.isReachable()超时 [英] Java InetAddress.isReachable() timeout

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

问题描述

我试图找出网络上的特定主机是否可以访问。
我的java代码如下:

I am trying to find out if specific hosts on my network are reachable. My java code is as follows:

InetAddress adr = InetAddress.getByName(host);
if(adr.isReachable(3000)){
    System.out.println(host + " is reachable");
}

这很有效,但如果我将超时降低到500毫秒,它不会再指定主机可达。我计划在循环中检查很多主机,因此具有低超时非常重要。如果我从windows命令行手动ping主机,则需要不到10ms。

This works quite well, however if I lower the timeout to say 500ms instead, it will not designate the host reachable anymore. I plan to check quite a few hosts in a loop, so having a low timeout is quite important. If I ping the host manually from the windows command line, it takes less than 10ms.

那么为什么Java方法需要更高的超时才能成功?
有没有其他选择使用 isReachable()

So why does the Java method need a much higher timeout to succeed? Are there any alternatives to using isReachable()?

推荐答案

这取决于你的可达性是什么意思。如果只查找可访问主机侦听特定端口的内容,则可以打开到该端口的套接字连接(例如,通过检查端口80查找所有HTTP服务器)。使用InetAddress.isReachable()是依赖于实现的。根据javadoc,典型的实现将使用ICMP ECHO REQUESTs。使用Java NIO(非阻塞I / O)的已知端口检查(如http(80),smb(445)等)可以具有更高的性能。我公司的产品使用已知端口扫描来查找运行Telnet或SSH的盒子,使用NIO,我们可以扫描大约5000 IP /秒。

It depends on what you mean by reachability. If you only what do find reachable hosts listening on specific ports, you can open a socket connection to that port (for example, finding all HTTP servers by checking port 80). Using InetAddress.isReachable() is implementation dependent. According to the javadoc, "A typical implementation will use ICMP ECHO REQUESTs". A "known port" check (like http(80), smb (445), etc.) using Java NIO (non-blocking I/O) can have higher performance. My company has a product that uses a "known port" scan to find boxes running Telnet or SSH, using NIO, and we can scan about 5000 IP/sec.

这篇关于Java InetAddress.isReachable()超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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