繁体   English   中英

关闭连接后,Socket和SwingWorker无法正常工作

[英]Socket and SwingWorker doesn't work after closing the connection

我正在运行一个TCP / IP套接字,该套接字发送SOAP消息,然后获取响应,然后读取它。

问题在于这种情况:起初一切都很好,我发送了一条消息,然后使用swingworker得到了响应。 如果我关闭插座,然后尝试再次连接,我会通过布尔值停止回转工人。 当我再次连接时,我让线程运行,但是当我发送SOAP消息时,套接字没有得到任何输出,但是当我当时进行调试并逐步执行代码时,得到了响应和输出! 怎么会这样呢?

这是我的代码:

protected Object doInBackground() throws Exception {

        Integer result = 1;
        while (true) {

            if (startReading && !this.server.isSocketClosed()) {
                // send the SOAP Message based on which message is selected
                SendSOAPRequestMessage();
                //Thread.sleep(5);
                String responseMessage = null;
                try {
                    // get the response from the client/server
                    responseMessage = Utils.convertStreamToString(this.server);
                    System.out.println(responseMessage);
                    // give the message without the header + and check the content length and if the header is corrupted
                    fullMsg = decoder.DecodeSoapHeader(new String(responseMessage.getBytes("UTF-8")));
                } catch (Exception ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);

                }


            }

        }
}

如前所述这里 ,“SwingWorker的被设计为只执行一次。” 而且,您的工作程序不会同步对this.server访问,因此外部更改对工作程序的后台线程可能不可见。 一些替代方案:

  • 为每个请求创建一个新的worker实例。

  • 让工人管理套接字。

附录: 对于第一种解决方案,我也应该创建一个新的套接字吗?

号如前所述这里 ,“一个电话,开始对一个线程的之前发生在启动线程的任何行动。” 例如 ,将对套接字的引用作为构造函数参数传递可能更清楚。

另一方面,套接字开销可能无关紧要。 个人资料可以肯定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM