带有swing gui的java套接字连接冻结

bvpmtnay  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(262)

我对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

swing gui代码

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是冻结的
连接客户端之前和期间
在客户机写了一些东西,套接字关闭之后,所有的东西都会立即输出

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题