Last active
February 26, 2016 10:11
-
-
Save lingz/e25914bea8019e08a1cf to your computer and use it in GitHub Desktop.
Java + Android - Check if host is available
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean hostAvailable(String host, int port) { | |
try (Socket socket = new Socket()) { | |
socket.connect(new InetSocketAddress(host, port), 2000); | |
return true; | |
} catch (IOException e) { | |
// Either we have a timeout or unreachable host or failed DNS lookup | |
System.out.println(e); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment