title: java nio cannot read after register a write operation
body: <p>I got a problem when using Java nio to do networking.</p>
  
  <p>I start a server in a thread and register read operationsin the selector. and another thread connect to the server.</p>
  
  <ol>
  <li>client write a byte to the server</li>
  <li>server reads the datas and back a byte to the client</li>
  <li>client get the response and write a byte to the server again.</li>
  <li>server loop step 2.</li>
  </ol>
  
  <p>but server cannot do the step 4 successfully because I can't get any read Key from selector any more. why ?? I have registered it before, and client has written data.</p>
  
  <p>there is my code below : </p>
  
  <pre><code>package test;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.nio.ByteBuffer;
  import java.nio.channels.SelectionKey;
  import java.nio.channels.Selector;
  import java.nio.channels.ServerSocketChannel;
  import java.nio.channels.SocketChannel;
  import java.util.Iterator;
  
  
  public class JavaNIO {
  
      public static void main(String[] args) throws Exception {
          final Selector selector = Selector.open();
          ServerSocketChannel serverChannel = ServerSocketChannel.open();
          serverChannel.configureBlocking(false);
          serverChannel.socket().bind(new InetSocketAddress(5555));
          serverChannel.register(selector, SelectionKey.OP_ACCEPT);
          new Thread() {
              public void run() {
                  while(true) {
                      try {
                          long count = selector.select();
                          if(count == 0) continue;
                          Iterator&lt;SelectionKey&gt; itKey = selector.selectedKeys().iterator();
                          while(itKey.hasNext()) {
                              SelectionKey key = itKey.next();
                              itKey.remove();
                              if(key.isAcceptable()) {
                                  ServerSocketChannel channel = (ServerSocketChannel)key.channel();
                                  SocketChannel socket = channel.accept();
                                  socket.configureBlocking(false);
                                  socket.register(selector, SelectionKey.OP_READ);
  
                              }
                              if(key.isReadable()) {
                                  ByteBuffer buffer=  ByteBuffer.allocate(1);
                                  SocketChannel channel = (SocketChannel)key.channel();
                                  while(true) {
                                      buffer.flip();
                                      int len = channel.read(buffer);
                                      if(len == 0) break;
                                      buffer.clear();
                                  }
                                  channel.read(buffer);
                                  channel.register(selector, SelectionKey.OP_WRITE, null);
                              }
                              if(key.isWritable()) {
                                  SocketChannel channel = (SocketChannel) key.channel();
                                  channel.write(ByteBuffer.wrap("ce".getBytes()));
                                  key.cancel();
                              }
                          }
                      } catch(Exception e) {
                          e.printStackTrace();
                      }
                  }
              }
          }.start();
          new Thread() {
              public void run() {
                  try {
                      Socket socket = new Socket("localhost", 5555);
                      byte[] reads = new byte[1024];
                      for(int i = 0; i &lt; 1000; i++) {
                          socket.getOutputStream().write(new byte[]{1});
                          socket.getInputStream().read(reads);
                          System.out.println(new String(reads));
                      }
                  } catch (UnknownHostException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }.start();
      }
  }
  </code></pre>