I’ve created a simple method in attempts to determine if a socket is open on a remote machine. Here is my code:
public static Boolean isPortAvailable( int port, String bindAddr ) {
try {
System.out.println("IP: " + InetAddress.getByName(bindAddr ));
ServerSocket srv = new ServerSocket(port, 0, InetAddress.getByName(bindAddr ) );
srv.close();
srv = null;
return true;
} catch (IOException e) {
return false;
}
}
Passing it these two args:
String bindAddr = "remotemachinename";
int port = 1719;
It continues to tell me the port is not available but if I try netstat -a on the machine I see it’s clearly NOT in use. Am I missing something?
If you want to contact a listening (server) socket you should use a
Socketnot aServerSocket. See the ServerSocket javadoc, the third parameter of the ServerSocket constructor is a local address to bind to, not a remote address to connect to.Try this: