Starting ServerSocket on Java with User Interface(Swing) Freezes

这一生的挚爱 提交于 2019-11-26 18:39:09

问题


Good day,

I have an infinite loop for a ServerSocket, working fine... The problem is when I try to start the ServerSocket with a button. My user interface "Freeze" don't move, anything, but the server is up and fine, here I have a ScreenShot:

http://i.gyazo.com/15d331166dd3f651fc7bda4e3670be4d.png

When I press the button "Iniciar" means Start server, the User Interface Freezes (ServerSocket infinite loop). I can't change my code because its working fine.

    public static void iniciarServer() {

    try {
        appendString("\nServidor iniciado.");
        System.out.println("asdasd");
    } catch (BadLocationException e1) {
        e1.printStackTrace();
    }

    try {
        ss = new ServerSocket(1234, 3);
        while (true) {
            System.out.println("Esperando conexiones...");
            appendString("\nEsperando conexiones...");
            Socket s = ss.accept();
            System.out.println("Conexión entrante: " + s.getRemoteSocketAddress());
            appendString("\nConexión entrante: " + s.getRemoteSocketAddress());

            conexiones++;
            //System.out.println("Debug: conexiones SERVER: " + conexiones);
            MultiThread mt = new MultiThread(s, conexiones);
            mt.start();
            ///////////////////////////////////////////////////////////////
        }
    } catch (IOException e) {
        System.out.println("Error Server: " + e.getMessage());
    } catch (BadLocationException e) {
        e.printStackTrace();
    } 
    stopServer();
}

appendString(); Is for add some text to the JTextPane, but doesnot work because the UI freezes.

Is there any way to do an user interface that don't freeze by the infinite loop?

Thanks!


回答1:


Swing is a single threaded framework, meaning any blocking or long running operation executed within the context of the Event Dispatching Thread will prevent it from processing the Event Queue, making your application hang.

It's also not thread safe, so you should never try and modify the state of any UI component from out side of the EDT.

Take a look at Concurrency in Swing and Worker Threads and SwingWorker for more details

public class ServerSocketWorker extends SwingWorker<Void, String> {

    private JTextArea ta;

    public ServerSocketWorker(JTextArea ta) {
        this.ta = ta;
    }

    @Override
    protected void process(List<String> chunks) {
        for (String text : chunks) {
            ta.append(text);
        }
    }

    @Override
    protected Void doInBackground() throws Exception {
        ss = new ServerSocket(1234, 3);
        while (true) {
            publish("\nEsperando conexiones...");
            Socket s = ss.accept();
            publish("\nConexión entrante: " + s.getRemoteSocketAddress());

            conexiones++;
            //System.out.println("Debug: conexiones SERVER: " + conexiones);
            MultiThread mt = new MultiThread(s, conexiones);
            mt.start();
            ///////////////////////////////////////////////////////////////
        }
    }

    @Override
    protected void done() {
        stopServer(); //??
    }
}

To start it, you could use something like...

public void iniciarServer() {
    ServerSocketWorker worker = new ServerSocketWorker(textAreaToAppendTo);
    worker.execute();
}

As an example




回答2:


The method ServerSocket.accept() is a blocking method. This means that Socket s = ss.accept(); stops the current thread until a connection to the server socket is opened.

Event dispatching in Swing is single threaded, the while loop and the blocking operation mentioned above, will keep the thread 'busy' and block all other interactions with the UI.

You should run your entire while loop in a separate thread. When you want to stop the server, you should also ensure that the while loop is exited and the thread completes execution.



来源:https://stackoverflow.com/questions/29935628/starting-serversocket-on-java-with-user-interfaceswing-freezes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!