title: How to create user groups/chatrooms in tcp sockets
body: <p>I have a tcp chat server in which I can handle multiple clients using threads. At first I started with a single room (a client message received by server will be broadcasted to other clients).</p>
  
  <p>Now I am handling 5 rooms in each thread with their own set of global maps with listeners(socket.getoutputstream) as key and username as value. But it is not the way I want. I did not used room threads because its difficult to pass different types of data to them while they are running. In future maybe I will expand rooms but I can't figure it out how it should be done. Code looks like this:</p>
  
  <pre><code>// dummy part of clientthread 
  public void room1(BufferedReader in, PrintWriter out,String username) { 
      room1map.put(out, username); 
      while(true){
          String message = in.readline;
          if(message==null){ 
              room1map.remove(out) 
              return;
          } 
          for(PrintWriter writer : room1map.keySet()){ 
              writer.println(username + ": " + message)
          } 
      }
  }
  </code></pre>