I Überprüfung bin für die Verfügbarkeit eines TCP-Port wie folgt aus:Java - Überprüfen Sie für TCP-Port-Verfügbarkeit
public static boolean isTCPPortAvailable(int hostPort) {
ServerSocket tempSocket = null;
try {
tempSocket = new ServerSocket();
// Enable Reuse before binding
tempSocket.setReuseAddress(true);
tempSocket.bind(new InetSocketAddress(hostPort));
return true;
}
catch (IOException ex) {
// Denotes that port is in use
}
finally {
if(tempSocket != null) {
try {
tempSocket.close();
}
catch (IOException e) {
// Not to be thrown
}
}
}
return false;
}
Das Problem ist, dass diese Methode true zurückkehrt, auch wenn Port in LISTENING
Zustand befindet. Kann mir jemand helfen, das Problem zu finden?
HINWEIS: Ich verwende setReuseAddress(true)
, um den Status TIME_WAIT
zu ignorieren.
HINWEIS: Ich bin auf Windows.
Down Wähler können Sie bitte Ihre Gründe kommentieren? –