title: java scanner blocks at .next() when it shouldn't
body: <p>I'm creating a simple Server/Client application. The Server wraps the <code>socket.getInputStream()</code> in a <code>Scanner</code>. Client takes user input from <code>System.in</code> and sends it to the Server with a <code>PrintWriter</code>. The server evaluates <code>scanner.hasNext()</code> to true, but it blocks at the following <code>scanner.next()</code> call. As per the javadocs: </p>
  
  <blockquote>
    <p>This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.</p>
  </blockquote>
  
  <p>well.. how does this make any sense? <strong>EDIT:</strong> it is worth noting that when the client terminates, the server now proceeds with the call to <code>scanner.next()</code>. Bonus question: the Client instanciates <code>PrintWriter</code> with the <code>autoFlush</code> parameter as <code>true</code>, yet I am required to flush after each call to <code>.write()</code>. Why? Here's an MCV of my code:</p>
  
  <pre><code>import java.io.IOException;
  import java.net.ServerSocket;
  import java.util.Scanner;
  
  public class MCVServer {
      public static void main(String[] args) throws IOException {
          ServerSocket server = new ServerSocket(1337);
          Scanner fromClient = new Scanner(server.accept().getInputStream());
          while (fromClient.hasNext()) {
              System.out.println("server heard something: ");
              System.out.println(fromClient.next());
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Scanner;
  
  public class MCVClient {
  
      public static void main(String[] args) throws UnknownHostException, IOException {
          Socket socket = new Socket("localhost", 1337);
          Scanner input = new Scanner(System.in);
          PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
  
          while(true) {
              output.write(input.next());
              output.flush();
          }
      }
  }
  </code></pre>