【问题标题】:Server-GUI freezes when waiting for connections等待连接时服务器-GUI 冻结
【发布时间】:2017-12-08 16:50:32
【问题描述】:

我正在尝试创建一个多线程服务器,通过客户端连接到它工作正常,但 GUI 被冻结。可能因为它一直在监听传入的连接,所以它卡在了一个 while 循环中。那么,我是否需要为多线程服务器创建一个线程(它本身将创建线程)或者我将如何解决这个问题?

多线程服务器:

public class MultiThreadServer{

    public static final int PORT = 2000;
    //function below is called by the GUI. 
    public void startServer() throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Server socket: " + s);
        System.out.println("Server listening...");
        try { while(true) {
            // Blocks until a connection occurs:
            Socket socket = s.accept();
            System.out.println("Connection accepted.");
            System.out.println("The new socket: " + socket);
            try {
                ClientThread t = new ClientThread(socket);
                System.out.println("New thread started.");
                System.out.println("The new thread: " + t);

                }
                catch(IOException e) {
                    System.out.println("===Socket Closed===");
                    socket.close();
                }
            } 
        } 
        finally { System.out.println("===ServerSocket Closed==="); s.close(); }
        } 
    } 
}

服务器界面:

public class ServerGUI extends Application implements Initializable{

    @FXML private MenuItem startConn;

    @Override
    public void start(Stage stage) throws Exception {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("ServerGUI.fxml"));
            stage.setScene(new Scene(root));
            stage.show();
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {

        startConn.setOnAction(e -> {
            try {
                MultiThreadServer mts = new MultiThreadServer();
                mts.startServer();
                System.out.println("Starting Server...");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });
    }
}

【问题讨论】:

  • 那么,我需要为多线程服务器创建一个线程 - 是的。服务器类调用accept(),这是一种阻塞方法:如果您不在单独的线程中调用它,它将冻结您的应用程序。
  • 移动 s.accept 和 while 循环进入 ClientThread 运行方法。

标签: java multithreading


【解决方案1】:

我怀疑,服务器需要一个新线程。

多线程服务器:

public Runnable startServer() throws IOException {

在创建函数Runnable后我添加了这个:

服务器界面:

startConn.setOnAction(e -> {
            Thread t1 = new Thread(new Runnable() {
                public void run()
                {
                    MultiThreadServer mts = new MultiThreadServer();

                    try {
                        mts.startServer();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }});  
                t1.start();

感谢用户 BackSlash 提供参考链接。

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2013-02-24
    • 2019-05-10
    相关资源
    最近更新 更多