How to stop ServerSocket Thread correctly? Close Socket failed

拜拜、爱过 提交于 2020-01-03 00:59:13

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/5569226/how-to-stop-serversocket-thread-correctly-close-socket-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!