What is the quickest way to detect an unreachable host in Java?

后端 未结 6 928
野性不改
野性不改 2021-02-08 01:38

I would like the fastest and most accurate function boolean isReachable(String host, int port) that passes the following JUnit tests under the conditions below. Tim

6条回答
  •  死守一世寂寞
    2021-02-08 02:26

    Firstly you need to recognise that you have potentially conflicting requirements; IP sockets are not time deterministic. The quickest you can ever detect unreachability is after your elapsed timeout. You can only detect reachability quicker.

    Assuming reachability/isReachable is your real objective, you should just use a straightforward non-blocking socket IO as shown in the Java Ping simulator, the example connects to the time service but would work equally well on 8080.

提交回复
热议问题