title: Setting up a multi-client/server TCP connection in Java
body: <p>I'm trying to create a multi-client/server application in Java. I'm having quite some issues because my threads seem to get entangled... Here's what I'm trying to do.</p>
  
  <ol>
  <li><p>I have a Server-class that accepts clients by using this piece of code:</p>
  
  <p>while(true){
              Socket socket = serverSocket.accept();
  }</p></li>
  <li><p>My server should remember the clients that are connected so I create a new Thread called ClientThread with that socket and place that Thread in a List on the Server</p></li>
  <li><p>That Thread listens to Command-objects that are sent from the client. If it receives a Command, it needs to send it to the Server instance for further processing (without creating a new instance of Server on that ClientThread). I've tried to do this by adding the Server-instance to this Thread when it's created. (Is this the right way?)</p></li>
  <li><p>My Server should also be able to send Objects back to the clients (1 or more) at any time. I'm trying to do this by using <code>socket.getOutputStream()</code> that is kept in the ClientThread</p></li>
  </ol>
  
  <p>How should I organize my Threads so that every client is constantly listening to accept objects from the server AND that they can send objects to the Server at any time.</p>
  
  <p>I know this isn't really a specific question, but if you know some info or tutorials that might be helpful <strong>for this use case</strong>, I'd be really thankful.</p>
  
  <p>btw: I know how to create sockets and send (serializable) objects and so on..  I'm just stuck on how to organize everything</p>