Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You must not simply stop the server. The shutdown process might take a while while cleanup occurs because you need to ensure consistency. </p> <p>Imagine a database server, if you simply shut it down while it is carrying out transactions you may leave its data inconsistent. That's why it typically takes a while to shutdown the server.</p> <ul> <li>You must first stop accepting new connections in the server. </li> <li>Then you can either wait for the current worker threads to finish their work and then close the server and shutdown officially.</li> <li>Or you force the worker threads to close their connections with the<br> client (probably using some sort<br> of flag as suggested). This might imply some cleanup to leave data consistent, for instance revert trasnsactions or any kind of changes you have done in files or in memory.</li> </ul> <p>Closing the connections with the clients in the server side should cause the clients to get a EOF on their sides as far as I understand.</p> <p><strong>[EDIT-1]</strong></p> <p>I have delved a bit on the issue, just because I had not used sockets in a while and because I found the question interesting. I think as it has been well pointed out by others, the only option is to close the socket, which according got Javadocs will automatically close the input and output streams/</p> <p>If there are chances that the thread is not IO-blocked, but in wait state or sleeping, I think it is still recommended to issue a Thread.interrupt() for the corresponding worker thread of a given socket; because there cannot be certainty of the blocking of state of every thread.</p> <pre><code>public static class IOServerWorker implements Runnable{ private Socket socket; public IOServerWorker(Socket socket){ this.socket = socket; } @Override public void run() { String line = null; try{ BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while( (line = reader.readLine())!=null){ System.out.println(line); } reader.close(); }catch(IOException e){ //TODO: do cleanup here //TODO: log | wrap | rethrow exception } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload