1

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!

Gray
  • 115,027
  • 24
  • 293
  • 354
nyyrikki
  • 1,436
  • 1
  • 21
  • 32

1 Answers1

5

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.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • sounds plausible (<-- is that a real english word?) to me, will try it tomorrow when i am working on the code again. thanks again for help. – nyyrikki Apr 06 '11 at 16:28
  • if a VM exists, all sockets are closed. – irreputable Apr 06 '11 at 16:48
  • 1
    @irreputable can you explain what you mean by that? The port used by the ServerSocket is not automatically free for re-use if the JVM exits, see http://stackoverflow.com/questions/767292/how-do-i-close-a-port-in-a-case-of-program-termination/767317#767317 – matt b Apr 06 '11 at 17:16
  • I have never experienced that - server socket remains listening for minutes(?!) after VM exit. since that state is after `close()`, your remedy of invoking `close()` before VM exit won't make a difference. in any case, VM exit triggers the same `close()` that you want to make sure get called. – irreputable Apr 06 '11 at 17:47