Client-Server Java GUI: read/write causing program to freeze

前端 未结 2 1019
半阙折子戏
半阙折子戏 2021-01-27 01:47

I\'m doing a client/server program in Java (including a GUI). I\'ve got the following code in the client:

public class SBListener implements ActionListener{
  pu         


        
相关标签:
2条回答
  • 2021-01-27 02:26

    Don't execute client/server code in an ActionListener. This will cause the Event Dispatch Thread to block while waiting for a response from the server. When EDT is blocked the whole GUI freezes.

    Read the section from the Swing tutorial on Concurrency for more information. You need so use a separate Thread for the client/server code. Or you can use a SwingWorker as discussed in the tutorial.

    0 讨论(0)
  • 2021-01-27 02:39

    My guess is that outToServer isn't being flushed. I would guess (although I can't tell from your sample code) that outToServer is a DataOutputStream. You need to call .flush to get the data out of the buffer and onto the wire.

    Try this:

    outToServer.writeUTF(usn.getText().trim());
    outToServer.flush();
    
    0 讨论(0)
提交回复
热议问题