Java InetAddress.isReachable () timeout

I am trying to find out if certain hosts are available on my network. My Java code is as follows:

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

This works pretty well, however, if I reduce the timeout to say 500 ms, it will no longer determine the availability of the node. I plan to test quite a few hosts in a loop, so having a low timeout is very important. If I ping the host manually from the Windows command prompt, it takes less than 10 ms.

So, why does the Java method require a much higher timeout? Are there alternatives to using isReachable() ?

+8
java ping
source share
1 answer

It depends on what you mean by achievement. If you only find that available hosts are listening on specific ports, you can open a socket connection to that port (for example, find all HTTP servers by setting port 80). Using InetAddress.isReachable () is implementation dependent. According to javadoc, "A typical implementation will use ICMP ECHO requests." Checking a "known port" (for example, http (80), smb (445), etc.) Using Java NIO (non-blocking I / O) can have better performance. My company has a product that uses β€œknown port” scanning to search for mailboxes using Telnet or SSH using NIO, and we can scan about 5000 IP / sec.

+5
source share

All Articles