0

I recently came across two methods for checking whether a ip-address is reachable or not:

1) Using java.net.Socket 2) Using java.net.InetAddress

Using (1), opens a TCP connection at port 80, whereas using (2) would typically use ICMP echo requests (if permitted by the server) or open a TCP connection on port 7.

Which of these two methods would be fast? I have a number of servers to ping from my java code, so how would I go about doing that?

Any help would be appreciated!

Bey0ndZ
  • 114
  • 3
  • Why do you need to check if an IP address is reachable? – user253751 Jul 09 '14 at 20:17
  • I need to know which IP addresses are accessible (server: live) and which are not. – Bey0ndZ Jul 09 '14 at 20:20
  • They both accept timeouts, so they're both as fast as you want them to be. You can speed things up by doing the connection attempts in parallel; one way is to submit each attempt to an [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) (usually obtained from a factory method in [Executors](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html)). – VGR Jul 09 '14 at 21:27
  • (2) uses ICMP echo requests if permitted by the *localhost* and you're not on Windows. You need superuser permission on Unix and Linux. In practice this means it uses TCP port 7. If you're interested in servers rather than just entire hosts you should be testing port 80, or whatever other port(s) the servers are listening at. – user207421 Jul 10 '14 at 00:27
  • Most of the servers that I'm pinging to respond (I'm on Ubuntu). But I am getting false positives for few servers, using the isReachable() method of InetAddress. I guess, I would be better off using Sockets. One more thing: What if I open up a TCP connection on port 80 on multiple servers from my system? Wouldn't it be potentially risky on a corporate network? – Bey0ndZ Jul 10 '14 at 17:29

0 Answers0