【问题标题】:Running a ServerSocket in a SwingWorker in Java GUI在 Java GUI 中的 SwingWorker 中运行 ServerSocket
【发布时间】:2018-08-30 19:25:35
【问题描述】:

我已经尝试了很长时间才能使以下代码正常工作,但没有成功。我想打开一个 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 冻结,因为它不是线程安全的。

【问题讨论】:

  • 好吧,doInBackground() 没有代码。你没有被卡住,你是在要求我们为你写整件事。需要更多地尝试实际代码。
  • 问题是我不知道该怎么做。试了很久没有用。我真的很感激一些帮助。我在互联网上找不到任何关于此主题的有用文章。

标签: java multithreading user-interface serversocket swingworker


【解决方案1】:

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

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

当按下开始按钮时,创建一个线程,保存对它的引用,然后启动它。 当按下停止按钮时,向该线程发送中断信号。 就是这样。不需要 SwingWorker。

【讨论】:

  • 是的,我确实想通了。我现在知道它在单独的线程上创建了一个服务器。
猜你喜欢
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多