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

后端 未结 6 944
野性不改
野性不改 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:21

    My most recent solution depends using a TimedSocket (source code) with 3000ms timeout while performing a connect.

    Timings:

    • 1406ms : testLocalHost()
    • 5280ms : testLAN()

    Can't even get these to work properly:

    • testNoDNS()
    • testHaveDNS()

提交回复
热议问题