在Java GUI中在SwingWorker中运行ServerSocket

问题描述 投票:0回答:1

我一直在努力让以下代码工作,但没有成功。我想打开一个ServerSocket,让它在我的GUI界面的后台运行。我有一个应该启动服务器的启动按钮和一个停止按钮,它将阻止服务器进一步监听希望连接的客户端。

这是一个非常基本的GUI,有2个按钮和2个文本字段。用户需要输入与数据库匹配的用户名和密码。如果细节正确,则应启动服务器。但是,如果服务器未在单独的线程上运行,则会导致GUI冻结。我希望使用SwingWorker来做到这一点。 (除非有更好的方法)

然而,我对这件事的知识有点深入。任何帮助将非常感激。谢谢!

public class Server extends JFrame implements ActionListener {

JLabel instructionsLabel;
JLabel passwordLabel;
JPasswordField passwordTF;
JButton shutdownButton;
JButton startupButton;
JLabel usernameLabel;
JTextField usernameTF;

public Server() {
    super("Server");
    initComponents();
}



public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    String f = "n";
    ConnectionBean cb = new ConnectionBean();

    char[] a = passwordTF.getPassword();
    String b = new String(a);
    String inputDetails = usernameTF.getText() + b;

    Iterator it = cb.getDetails().iterator();
    while (it.hasNext()) {
        Object next = it.next();
        if (inputDetails.equals(next)) {
            f = "y";
            if (source == startupButton) {
                try {
                    sop.doInBackground();
                } catch (Exception ex) {
                }
                JOptionPane.showMessageDialog(null,
                        "Congratulations! Server started.",
                        "Start-up Message",
                        JOptionPane.INFORMATION_MESSAGE);
            } else if (source == shutdownButton) {
                try {
                    sop.failIfInterrupted();
                } catch (Exception ex) {
                }
                JOptionPane.showMessageDialog(null,
                        "Server shut-down successfully!",
                        "Shut-down Message",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
    if (f.equals("n")) {
        JOptionPane.showMessageDialog(null, "Invalid username or password.", "Alert", JOptionPane.WARNING_MESSAGE);
    }
    passwordTF.setText("");
    usernameTF.setText("");
    cb.setCloseConnection(true);
}

// THIS IS WHERE I GET STUCK!

public class ServerOperation extends SwingWorker<Void, Void> {

    ServerSocket ss;
    Socket cs;

    public ServerOperation(ServerSocket ss) {
        this.ss = ss;
    }

    @Override
    protected Void doInBackground() throws Exception {

        return Void;
    }

    public void failIfInterrupted() throws InterruptedException {
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedException();
        }
    }
}

我之前用来启动服务器的代码就是这个......

ServerSocket ss;
Socket cs;

public void StartingSer() throws Exception {

    ss = new ServerSocket(7777);

    while (true) {
        cs = ss.accept();
        new ServerTread(cs).start();

    }
}

但是这会导致GUI冻结,因为它不是线程安全的。

java multithreading user-interface serversocket swingworker
1个回答
0
投票

“但是这会导致GUI冻结,因为它不是线程安全的”

不,这会导致GUI冻结,因为此方法在GUI线程上执行。您必须使用单独的线程。

按下“开始”按钮后,创建一个线程,保存对它的引用,然后启动它。按下“停止”按钮后,向该线程发送中断信号。而已。不需要SwingWorker。

© www.soinside.com 2019 - 2024. All rights reserved.