开发者

How to stop ServerSocket Thread correctly? Close Socket failed

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 clien开发者_如何转开发tConn = 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜