SwingWorker 并从套接字检索数据

发布于 2024-12-29 08:18:38 字数 2533 浏览 1 评论 0原文

我有这样的代码

try {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(888);
        } catch (IOException e) {}

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {}

        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;

        while ((inputLine = in.readLine()) != null) {
            outputLine = "SRV:>"+inputLine+"<:VRS";
            out.println(outputLine);
            taWyjscie.append(outputLine+"\n");
            if (inputLine.equals("Bye."))
                break;
        }

        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close(); 

    } catch (IOException ex) {
        taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
    }

它可以工作,但是应用程序在客户端发送“再见”之前无法使用。

我希望能够通过 gui 从服务器向客户端发送消息,所以我与 SwingWorker 进行斗争,我得到了这个:

try {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(888);
        } catch (IOException e) {}

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {}

        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;

        zadanie = new ServerTask(in);

    } catch (IOException ex) {
        taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
    }

 private class ServerTask extends SwingWorker<String, String> {
    private BufferedReader iin;

    public ServerTask(BufferedReader win) {
        this.iin = win;
    }


    @Override
    protected String doInBackground() throws Exception {
        String input;
        while ((input = iin.readLine()) != null) {
            System.out.println(input);
            publish(input);
            if (isCancelled())
                break;
        }
        return null;
    }
    @Override
    protected void process(List<String> chunks) {
        for(String el : chunks) {
            textAreaOUT.append(el+"\n");
        }
    }

}

现在我无法从服务器向客户端发送消息,但来自客户端的消息不会显示在 textArea 中。我从早上就开始奋斗,我不知道摇摆工人是如何工作的……

PS。前面的 try ... catch 位于按钮操作执行函数中(如果有任何区别)

I have code like this

try {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(888);
        } catch (IOException e) {}

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {}

        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;

        while ((inputLine = in.readLine()) != null) {
            outputLine = "SRV:>"+inputLine+"<:VRS";
            out.println(outputLine);
            taWyjscie.append(outputLine+"\n");
            if (inputLine.equals("Bye."))
                break;
        }

        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close(); 

    } catch (IOException ex) {
        taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
    }

It works, but app is unusable until client sends "Bye.".

I want to be able to send messages to client from server by gui, so im fighting with SwingWorker, i got this:

try {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(888);
        } catch (IOException e) {}

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {}

        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;

        zadanie = new ServerTask(in);

    } catch (IOException ex) {
        taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
    }

 private class ServerTask extends SwingWorker<String, String> {
    private BufferedReader iin;

    public ServerTask(BufferedReader win) {
        this.iin = win;
    }


    @Override
    protected String doInBackground() throws Exception {
        String input;
        while ((input = iin.readLine()) != null) {
            System.out.println(input);
            publish(input);
            if (isCancelled())
                break;
        }
        return null;
    }
    @Override
    protected void process(List<String> chunks) {
        for(String el : chunks) {
            textAreaOUT.append(el+"\n");
        }
    }

}

now i cant send message from server to client, but messages from client are not displayed in textArea. Im fighting with since morning and i cant get idea how swingworker is working...

PS. this try ... catch in front is in button actionperformed function (if it makes any differece)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2025-01-05 08:18:38

您从未在 SwingWorker 上调用 execute,这是您的第一个错误。从那里开始..

You never call execute on your SwingWorker, thats your first bug. Start there..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文