网上查了一下,当端口已经在使用时,会出现BindException。但我只使用过这个端口号一次。这是我的代码。
public class ServerUI extends Application {
private ServerSocket serverSocket = null;
public ServerUI() throws IOException {
this.serverSocket = new ServerSocket(1111);
}
@Override
public void start(Stage primaryStage) throws Exception {
ServerUI server = new ServerUI();
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = (Parent)loader.load();
Controller controller = (Controller)loader.getController();
primaryStage.setTitle("Server");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {launch(args);}
}
根据堆栈跟踪,当我尝试在第 10 行创建 ServerUI 实例时,错误发生在第 5 行。不知道如何调试这个。感谢帮助。
最佳答案
要释放 Windows 计算机上的端口,请以管理员身份运行命令提示符并执行以下命令:
netstat -o -n -a | findstr 0.0:1111 // finds the process using the port
taskkill /F /PID 10880 // change 10880 with your PID number which you can see from the previous command.
关于java.net.BindException : Address already in use (Bind failed) but port is only used once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43009139/