首页 > 解决方案 > 与 Swing GUI 的 Java 套接字连接冻结

问题描述

我对java很陌生,我想在客户端和服务器之间建立一个套接字连接。没有 GUI,它工作得很好。但是现在我已经尝试为程序实现 GUI,每当我尝试打开套接字时,服务器端界面都会冻结,即使程序仍在运行,所以只有界面冻结。每当客户端完成连接时,服务器 GUI 将立即输出所有冻结的内容。我做了一些测试,我认为是方法,server.accept()但我确定。

Client.java 代码

public void waiting(int port) {
    try
    {
        ServerWindow.consolePrint("Server starting up...", 2);
        System.err.print("hello world");
        server=new ServerSocket(port);
        ServerWindow.consolePrint("Server running!", 6);
        ServerWindow.consolePrint("Waiting for a client to connect...", 2);
        client= server.accept();
        ServerWindow.consolePrint("Client connected!", 6);
        
        server.close();
        
        inFromClient=new BufferedReader(new InputStreamReader(client.getInputStream()));
        outToClient=new DataOutputStream(client.getOutputStream());
        communicate();

    }
                   //catch code
}


public void communicate()
{
    
    try {
        outToClient.writeBytes("type /help to display all the commands"+'\n');
        ServerWindow.consolePrint("Waiting for a command from the client...", 2);
        stringReceived=inFromClient.readLine();
        ServerWindow.consolePrint(stringReceived, 1);
        client.close();

      }
              //catch code

摇摆图形用户界面代码

btnConnect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                //if - else if formatting check
               
                else {
                    server.waiting(Integer.parseInt(TxtPort.getText()));
                }

                   //Input metod for printing string into the console

public static void consolePrint(String strToConsole, int n) {
    SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    switch (n) {
    case 1: 
        // CLIENT MESSAGE
        StyleConstants.setForeground(style, Color.yellow);
         try {
                    doc.insertString(doc.getLength(), formatter.format(date) + " - Client: ",style);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
         StyleConstants.setForeground(style, Color.white);
         try {
                    doc.insertString(doc.getLength(), strToConsole + "\n" ,style);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
         break;
         
    
    case 2:
        // SERVER MESSAGE
        StyleConstants.setForeground(style, Color.cyan);
         try {
                    doc.insertString(doc.getLength(), formatter.format(date) + " - Server: ",style);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
         StyleConstants.setForeground(style, Color.white);
         try {
                    doc.insertString(doc.getLength(), strToConsole + "\n" ,style);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
         break;
         
    case 3:
        // CLIENT ERROR MESSAGE
                    //code
         break;
         
    case 4:
        // SERVER ERROR MESSAGE
         try {
                   //code
         break;
         
    case 5:
        // CLIENT SUCCESS MESSAGE
         try {
                  //code
         break;
         
    case 6:
        // SERVER SUCCESS MESSAGE
         try {
                 //code
         break;
        
    default:
        //code
    }
}

MainS.java 代码

public static void main(String[] args) {
        ServerWindow windowGUI = new ServerWindow();
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    windowGUI.frmServer.setVisible(true);
                    windowGUI.frmServer.setResizable(false);
                    windowGUI.frmServer.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

我正在链接 2 张图片以显示只有 UI 冻结

客户端连接之前和期间

在客户端写了一些东西并且套接字关闭之后,所有东西都会立即输出

标签: javaswing

解决方案


推荐阅读