title: How java nio ServerSocketChannel accept works?
body: <p>I can't get how NIO works under the hood. Here is a sample code:</p>
  
  <pre><code>// Create the server socket channel
  ServerSocketChannel server = ServerSocketChannel.open();
  // nonblocking I/O
  server.configureBlocking(false);
  // host-port 8000
  server.socket().bind(new java.net.InetSocketAddress(host,8000));
  
  // Create the selector
  Selector selector = Selector.open();
  // Recording server to selector (type OP_ACCEPT)
  server.register(selector,SelectionKey.OP_ACCEPT);
  
  while (true) {
        selector.select(); // blocking operation
        Iterator it = selector.selectedKeys().iterator();
        while (it.hasNext()) {
          SelectionKey selKey = (SelectionKey) it.next();
  
          // THE MOST INTRIGUING PART HERE!!!
          if (selKey.isAcceptable()) {
            ServerSocketChannel ssChannel = (ServerSocketChannel) selKey.channel();
            SocketChannel sc = ssChannel.accept();
          }
          it.remove();
       }
  }
  </code></pre>
  
  <p>Here I have a couple of questions:</p>
  
  <ol>
  <li>selKey.channel() return a ServerSocketChannel is it exactly the same channel we created in the beggining with ServerSocketChannel.open()? If not, then what is it?</li>
  <li>More important question: in most other tutorials selKey.channel(); step is skipped and they simply use SocketChannel client = server.accept(); For example here: <a href="http://www.onjava.com/pub/a/onjava/2002/09/04/nio.html?page=2" rel="noreferrer">http://www.onjava.com/pub/a/onjava/2002/09/04/nio.html?page=2</a> and here: <a href="http://www.developer.com/java/article.php/10922_3837316_2/Non-Blocking-IO-Made-Possible-in-Java.htm" rel="noreferrer">http://www.developer.com/java/article.php/10922_3837316_2/Non-Blocking-IO-Made-Possible-in-Java.htm</a> So, how does server.accept() knows about current key we process?</li>
  <li><p>In <a href="http://www.developer.com/java/article.php/10922_3837316_2/Non-Blocking-IO-Made-Possible-in-Java.htm" rel="noreferrer">http://www.developer.com/java/article.php/10922_3837316_2/Non-Blocking-IO-Made-Possible-in-Java.htm</a> they even suggest to accept channel in new thread. I guess it is possible that the following situation may occur.</p>
  
  <pre><code>key1 someClient1 acceptable
  key2 someClient2 not acceptable
  key3 someClient3 acceptable
  
  startThread1
  startThread3
  
  scheduler decides to give time to thread3 instead of thread1
  
  thread3 -&gt; socket.accept() &lt;- actually accepts client1
  thread1 -&gt; socket.accept() &lt;- actually accepts client3
  </code></pre></li>
  </ol>
  
  <p>So, could you please explain how selector works in pair with ServerSocketChannel and accept method? Because I don't understand in which order #accept accepts clients and how this order related to selectedKeys.</p>
  
  <ol start="4">
  <li><p>Can I simply do the following:</p>
  
  <pre><code>int availableClients = 0;
  while (it.hasNext()) {
      SelectionKey selKey = (SelectionKey) it.next();
  
      if (selKey.isAcceptable()) {
          ++availableClients;
      }
      it.remove();
  }
  for (int i = 0; i &lt; availableClients; ++i) {
         SocketChannel sc = server.accept();
         doSomething(sc);
  }
  </code></pre></li>
  </ol>