title: Java NIO Exception
body: <p>Hi I am trying to make a chat using NIO, in the server part in a separate thread from the main when I :</p>
  
  <pre><code>private void broadcast(String msg, String user)    
  {
      String Message = user + ":" + msg + "\0";
      System.out.println(Message);
      ByteBuffer buf=ByteBuffer.wrap(Message.getBytes());
      Set&lt;SelectionKey&gt; selectedKeys  = selector.keys();
      Iterator&lt;SelectionKey&gt; keyIterator = selectedKeys.iterator();
  
      while(keyIterator.hasNext()) 
      {
          SelectionKey key = keyIterator.next();
          if(key.attachment() != user)
          {
              try {
                  ((SocketChannel) key.channel()).write(buf);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              buf.rewind();
          }
          keyIterator.remove();
      }
  }
  </code></pre>
  
  <p>I get this exception:</p>
  
  <pre><code>java.lang.UnsupportedOperationException
  at java.util.Collections$UnmodifiableCollection$1.remove(Unknown Source)
  at chatserver.MessageSender.broadcast(MessageSender.java:41)
  at chatserver.MessageSender.run(MessageSender.java:113)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Line 41 is: keyIterator.remove();
  I am using this as guideline: <a href="http://tutorials.jenkov.com/java-nio/index.html" rel="nofollow">Java NIO Tutorial</a></p>