Skip to content

Instantly share code, notes, and snippets.

@lingz
Last active February 26, 2016 10:11
Show Gist options
  • Save lingz/e25914bea8019e08a1cf to your computer and use it in GitHub Desktop.
Save lingz/e25914bea8019e08a1cf to your computer and use it in GitHub Desktop.
Java + Android - Check if host is available
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