Question

My SwingWorker's doInBackground() creates (and registers with OP_ACCEPT) a fresh ServerSocketChannel when the user clicks the "Connect" button.

When a client isAcceptable(), the SwingWorker registers the SocketChannel with OP_READ.

When the user clicks the "Disconnect" button, the SwingWorker closes() the ServerSocketChannel and selector(). However, the client is still open.

Problem: if the user clicks "Connect" again, it seems to me that the process above repeats except that the client is still in OP_READ mode and isn't being freshly accepted by the ServerSocketChannel.

Is there a way to overcome this? Does a ServerSocketChannel restart require that clients restart as well?

Was it helpful?

Solution

Does a ServerSocketChannel restart require that clients restart as well?

No. The existing clients remain connected. They won't go through another connect phase just because you close the server socket.

Contrary to my comment above, the disconnect button should close the server socket, not the client socket(s). But it's mislabeled. The buttons should be 'start' and 'stop', or 'start listening' and 'stop listening'. It's the clients that do the connecting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top