0

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?

Vikdor
  • 23,934
  • 10
  • 61
  • 84
Arvanem
  • 1,043
  • 12
  • 22
  • I dont understand any of this. Why are you writing a GUI for a server? Why does the user get to control when the ServerSocket is created? When the user clicks the disconnect button, surely you should be closing the client connection, not the server socket? But then again why does the user get to control that too? – user207421 Sep 16 '12 at 00:47
  • @EJP Why is it so incomprehensible that the user of the server software can dictate when the server is to commence and stop? – Arvanem Sep 16 '12 at 00:51
  • @EJP But I take on board your suggestion the client connection should close! – Arvanem Sep 16 '12 at 00:52
  • Because there isn't such a thing as 'the user of the server' in most cases, nor a GUI either. The server starts, listens, accepts connections, handles them, disconnects them when it's finished with them, according to the application protocol, stops listening, and stops. What else is it doing before it starts listening and after it stops listening? – user207421 Sep 16 '12 at 02:03
  • The user of the server also has the choice of editing server settings (e.g host, port) and editing the master data table that clients access. See the screenshot here: http://stackoverflow.com/questions/12439005/how-to-structure-publish-and-process-in-a-swingworker-to-update-more-than-on – Arvanem Sep 16 '12 at 03:12

1 Answers1

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you for your correction to your comment. I agree now it is questionable whether there is any point in the user of the server application choosing to start or stop listening. – Arvanem Sep 16 '12 at 06:52