I have the need to discover open ports on a remote server. I'm wondering if this is possible. I was thinking I'd open a socket, and if this succeeds, it means that it's used ... otherwise, if I get an exception, then it is not used.
For example,
public boolean isActive() {
Socket s = null;
try {
s = new Socket();
s.setReuseAddress(true);
SocketAddress sa = new InetSocketAddress(this.host, this.port);
s.connect(sa, 3000);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
}
}
}
return false;
}
is this a viable approach?