title: Java NIO. SocketChannel.read method all time return 0. Why?
body: <p>I try understand how works java NIO. In particular, how works SocketChannel.</p>
  
  <p>I wrote code below:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.nio.*;
  import java.nio.channels.*;
  
  public class Test {
  
      public static void main(String[] args) throws IOException {
  
          SocketChannel socketChannel = SocketChannel.open();
          socketChannel.configureBlocking(false);
          socketChannel.connect(new InetSocketAddress("google.com", 80));
  
          while (!socketChannel.finishConnect()) {
              // wait, or do something else...
          }
  
          String newData = "Some String...";
  
          ByteBuffer buf = ByteBuffer.allocate(48);
          buf.clear();
          buf.put(newData.getBytes());
  
          buf.flip();
  
          while (buf.hasRemaining()) {
              System.out.println(socketChannel.write(buf));
          }
  
          buf.clear().flip();
  
          int bytesRead;
  
          while ((bytesRead = socketChannel.read(buf)) != -1) {
  
              System.out.println(bytesRead);
  
          }
  
      }
  
  }
  </code></pre>
  
  <ol>
  <li>I try connect to google server.</li>
  <li>Send request to the server;</li>
  <li>Read answer from the server.</li>
  </ol>
  
  <p>but, method socketChannel.read(buf) all time return 0 and performs infinitely. </p>
  
  <p>Where I made mistake??</p>