title: Using AsynchronousSocketChannel in Java
body: <p>When using AsynchronousSocketChannel in Java, how do we read() without calling these functions? I test read/write with netcat but it's supposed to be asynchronous so I would assume that a call to read() will not terminate. Should I be using it differently? If I use read() in a loop, it gives an exception "java.nio.channels.ReadPendingException" which makes me think that the first read() was not completed but the second tried to read() instead of the first read() waiting to complete.</p>
  
  <p>I am trying to understand a snippet I found on a site but I modified it to be simpler: </p>
  
  <pre><code>public class EchoClient {
  
      public AsynchronousSocketChannel sockChannel;
  
      public EchoClient(String host, int port) throws IOException {
          sockChannel = AsynchronousSocketChannel.open();
  
          sockChannel.connect( new InetSocketAddress(host, port), sockChannel, new CompletionHandler&lt;Void, AsynchronousSocketChannel &gt;() {
              @Override
              public void completed(Void result, AsynchronousSocketChannel channel ) {
                  System.out.println( "success");
              }
  
              @Override
              public void failed(Throwable exc, AsynchronousSocketChannel channel) {
                  System.out.println( "failed to connect to server");
              }
  
          });
      }
  
      public void startRead() {
          final ByteBuffer buf = ByteBuffer.allocate(2048);
  
          sockChannel.read( buf, sockChannel, new CompletionHandler&lt;Integer, AsynchronousSocketChannel&gt;(){
  
              @Override
              public void completed(Integer result, AsynchronousSocketChannel channel) {
                  //print the message
                  System.out.println( "Read message:" + new String( buf.array()) );
              }
  
              @Override
              public void failed(Throwable exc, AsynchronousSocketChannel channel) {
              }
  
          });
  
      }
  
      public void write(final String message) {
          ByteBuffer buf = ByteBuffer.allocate(2048);
          buf.put(message.getBytes());
          buf.flip();
          sockChannel.write(buf, sockChannel, new CompletionHandler&lt;Integer, AsynchronousSocketChannel &gt;() {
              @Override
              public void completed(Integer result, AsynchronousSocketChannel channel ) {
  
              }
  
              @Override
              public void failed(Throwable exc, AsynchronousSocketChannel channel) {
                  System.out.println( "Fail to write the message to server");
              }
          });
      }
  
  }
  </code></pre>
  
  <p>I call it in main() like this:</p>
  
  <pre><code>EchoClient echo = new EchoClient( "127.0.0.1", 3000, "echo test");
  echo.startRead();
  echo.write("hi");
  </code></pre>
  
  <p>With just this ^, the client terminates without bothering to read if the server didn't send anything yet</p>