如何正确停止ServerSocket线程?关闭套接字失败

发布于 2024-10-30 22:42:24 字数 2494 浏览 0 评论 0原文

我知道这个问题之前已经讨论过很多次了,但我找不到适合我的问题的解决方案。我想在后台运行一个 ServerSocket 线程,监听指定的端口。它确实有效,但只有一次。似乎服务器正在侦听的端口从未正确关闭,并且当我尝试重新启动时仍然处于活动状态(O 不要重新启动线程本身)。有人能说出为什么它不能正常工作吗?预先感谢您的帮助...!

编辑:

我在客户端也遇到同样的问题。我有一个发送者线程,而且该线程无法停止。最好的方法是什么?

ClientConnector 只是一个连接到服务器端口并发送数据的类。 它不是线程或类似的东西。

这是我的发送者类:

private class InternalCamSender extends Thread {

    private int sendInterval = 500;             // default 500 ms
    private ClientConnector clientConn = null;

    public InternalCamSender() {
        this.sendInterval = getSendingInterval();
        this.clientConn = new ClientConnector();
    }

    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            clientConn.sendCamPdu(CodingScheme.BER, createNewPDU());
            try {
                Thread.sleep(sendInterval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

我尝试像这样处理它的行为:

    if(jButton_startSending.getText().equals(STARTSENDING)) {
        new Thread() {
            public void run() {
                iSender = new InternalCamSender();
                iSender.start();
                jButton_startSending.setText(STOPSENDING);
            }
        }.start();
    } else {
        new Thread() {
            public void run() {
                if(iSender.isAlive()) {
                    iSender.interrupt();
                    try {
                        iSender.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                iSender = null;
                jButton_startSending.setText(STARTSENDING);
            }
        }.start();
    }

不知怎的,我无法像那样停止 InternalCamSender 。我之前尝试过使用易失性布尔值,结果是一样的。我读了 http://download.oracle.com/ javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html 页面并尝试了示例 我应该使用什么来代替 Thread.stop? 但即使这样也没有停止线程?我迷路了。

有什么想法吗?

编辑: 在这里找到了我的 clinet 发送问题的答案 http ://www.petanews.de/code-snippets/java/java-threads-sauber-beenden-ohne-stop/ 即使我也不知道为什么会这样。我确信我以前尝试过这种方式。

问题解决了!

I know this has been discussed some times before, but I can't find an appropriate solution for my problem. I want to run a ServerSocket thread in the background, listening to the specified port. It's working actually, but only once. Seems that the port the server is listening to is never closed correctly and still active when I try to restart (O don't restart the thread itself). Can some tell why it is not working correctly? Thanks in advance for any help...!

edit:

I have same problem on the client side. I have a sender thread and also that one cannot not be stopped. What is the best way to do that?

The ClientConnector is just a class which connects to the server port and sends the data.
It's not a thread or anything like that.

That's my sender class:

private class InternalCamSender extends Thread {

    private int sendInterval = 500;             // default 500 ms
    private ClientConnector clientConn = null;

    public InternalCamSender() {
        this.sendInterval = getSendingInterval();
        this.clientConn = new ClientConnector();
    }

    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            clientConn.sendCamPdu(CodingScheme.BER, createNewPDU());
            try {
                Thread.sleep(sendInterval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

And I try to handle it's behaviour like that:

    if(jButton_startSending.getText().equals(STARTSENDING)) {
        new Thread() {
            public void run() {
                iSender = new InternalCamSender();
                iSender.start();
                jButton_startSending.setText(STOPSENDING);
            }
        }.start();
    } else {
        new Thread() {
            public void run() {
                if(iSender.isAlive()) {
                    iSender.interrupt();
                    try {
                        iSender.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                iSender = null;
                jButton_startSending.setText(STARTSENDING);
            }
        }.start();
    }

Somehow I cannot stop the InternalCamSender like that. I tried with a volatile boolean before, was the same. I read the http://download.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html page and tried also the example What should I use instead of Thread.stop? but even that was not stopping the thread? I am lost.

Any ideas?

edit:
found the answer for my clinet sending problem here http://www.petanews.de/code-snippets/java/java-threads-sauber-beenden-ohne-stop/
even i don't know why that is working. I am sure I tried that way before.

Problem solved!

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

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

发布评论

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

评论(1

笨笨の傻瓜 2024-11-06 22:42:24

您应该在finally 块中关闭资源(流和套接字),而不是在catch 块中 - 这样,无论捕获异常还是异常,资源始终会关闭。不是。

从 catch 块内或线程内调用 System.exit() 也是一种不好的做法 - 在任何错误实例上都会强制关闭整个 JVM。这也可能是导致服务器套接字出现问题的原因 - 每当读取/关闭流时遇到任何异常时,您都会在有机会关闭服务器套接字之前退出 JVM。

You should close your resources (the streams and socket) in a finally block, rather than a catch block - this way the resources are always closed, whether an exception is caught or not.

It's also a bad practice to call System.exit() from within a catch block or within a thread - you are forcibly shutting down the whole JVM on any instance of an error. This is likely the cause of your problem with the server socket as well - whenever any exception is encountered with reading/closing the streams, you are exiting the JVM before you have a chance to close the server socket.

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