I need to open open ports on a remote server. I am wondering if this is possible. I thought I would open the socket, and if it succeeds, it means that it was used ... otherwise, if I get an exception, then it will not be 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 a viable approach?
java sockets
Jeffrey blattman
source share