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

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

    If you want to test whether you can connect to a web server you could also create a URL based on the host name and the port number and use that to create a URLConnection checking the result (including exceptions) of the connect method should tell you whether the webserver is reachable.

提交回复
热议问题