Arvanem
Arvanem

Reputation: 1053

Handling connection given a ServerSocketChannel restart when a client is already open

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?

Upvotes: 0

Views: 408

Answers (1)

user207421
user207421

Reputation: 310957

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.

Upvotes: 1

Related Questions