简体   繁体   中英

Starting ServerSocket on Java with User Interface(Swing) Freezes

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM