我正在设置邮件本地服务,并且已经使用JavaFX构建了服务器接口。
问题是,当我运行服务器时,它显示了界面,但是被阻止了,并且不能以任何方式工作。
似乎只有图形没有出现,后端正在工作。的确,如果我连接客户端并提出请求,那么一切都会正常。 可能是线程问题吗?
这是主要内容:
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainView.fxml"));
Parent root = loader.load();
stage.setTitle("Server Log");
stage.setScene(new Scene(root, 490, 309));
stage.centerOnScreen();
stage.show();
ServerModel m = new ServerModel(); //create Model
ServerController c = loader.getController(); //get controller
c.setModel(m);
m.start(); //the problem is here, if I comment this line It shows the server interface but obviously the backend doesn't work.
ServerModel的构造函数是这样的:
public ServerModel() {
try {
serversocket = new ServerSocket(5888);
} catch (IOException e) {
Logger.getLogger(ServerModel.class.getName()).log(Level.SEVERE, null, e);
}
}
这是该模型的启动方法,用于处理客户端对套接字的请求:
public void start(){
Socket socket = null;
while (!closed){ //server main loop
try{
socket = serversocket.accept();
//... (here there would be threads that communicate with client and set closed to true)
} catch (IOException e) {
e.printStackTrace();
}
}
}