如何正确停止ServerSocket线程?关闭套接字失败 [英] How to stop ServerSocket Thread correctly? Close Socket failed

查看:847
本文介绍了如何正确停止ServerSocket线程?关闭套接字失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...!

编辑

I在客户端有同样的问题。我有一个发送者线程,也不能停止。最好的方法是什么?

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?

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

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.

这是我的发件人类:

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 那样的。我尝试使用 volatile boolean 之前,是一样的。我阅读了 http://download.oracle.com/ javase / 1.5.0 / docs / guide / misc / threadPrimitiveDeprecation.html 页面并尝试了示例我应该使用什么而不是Thread.stop?但即使这样也没有停止线程?我迷路了。

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.

任何想法?

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

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.

问题解决了!

推荐答案

您应该在 finally 块中关闭资源(流和套接字),而不是 catch 块 - 通过这种方式,无论是否捕获到异常,资源始终都是关闭的。

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.

调用 System.exit()<也是一种不好的做法/ code>来自catch块或线程内 - 您在任何错误实例上强行关闭整个JVM。这可能也是服务器套接字问题的原因 - 只要遇到读取/关闭流的任何异常,就会在有机会关闭服务器套接字之前退出JVM。

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.

这篇关于如何正确停止ServerSocket线程?关闭套接字失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆