title: Java sockets: open connections from server to client
body: <p>I have a client server problem with the following unique requirement:
  1. The server has to open N connections to the known client and the client can only use those connections to request data from the server.</p>
  
  <p>So far I tried to do the following but ran into some issues:</p>
  
  <ol>
  <li><p>Have a socket listener on the client and have it enqueue each connection received into a ArrayBlockingQueue.</p></li>
  <li><p>Client app will use connections available in the queue above to request data from server</p></li>
  <li><p>I create one thread(worker) per socket on the server and listen for data requests through N worker threads.</p></li>
  <li><p>when I wait for incoming requests in the worker threads(on sever) I use InputStream.isAvailable() to check if there is something waiting to be read.</p></li>
  </ol>
  
  <p>Issue: the worker threads on the sever side don't know if socket connection is still live or not? They continue to listen for requests even after the sockets are closed on the client app.</p>
  
  <p>How can I make my server sense if the socket connection is still live or not ?</p>
  
  <p>Am I taking the right approach here?</p>
  
  <p>Is there a framework out there which will make the implementation easier and better ?</p>