title: java.io.BufferedReader.readLine() is nonblocking
body: <p>I have a problem with readLine() in Java. I have a server and a client. From client I want to send a message to the server. The problem is that first, the client has to insert a text into a JTextField and when presses send then server to read the input from client, but server doesn't wait the input from client but instead reads null. But I read that readLine() is blocked until it has something to read, why it's not happening in this case? </p>
  
  <p>Here I connect to the server and create the JFrame</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import javax.swing.JFrame;
  import javax.swing.SwingUtilities;
  
  public class StartingPoint {
  
  private static PrintWriter out;
  private static BufferedReader in;
  
  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              try {
                  connectToServer();
                  createAndShowGui();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  public static void createAndShowGui() throws IOException {
      View frame = new View(out, in);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
  }
  
  public static void connectToServer() throws IOException {
      String serverAddress = "127.0.0.1";
  
      int PORT = 8100;
  
      Socket clientSocket = null;
      out = null;
      in = null;
  
      try {
          clientSocket = new Socket(serverAddress, PORT);
          out = new PrintWriter(clientSocket.getOutputStream(), true);
          in = new BufferedReader(new InputStreamReader(
                  clientSocket.getInputStream()));
      } catch (UnknownHostException e) {
          System.err.println("Could not connect to the server \n" + e);
          System.exit(1);
      } finally {
          if (out != null)
              out.close();
          if (in != null)
              in.close();
          if (clientSocket != null)
              clientSocket.close();
      }
  }
  }
  </code></pre>
  
  <p>Here is JFrame implementation:</p>
  
  <pre><code>import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  public class View extends JFrame {
  
  private JButton button;
  private JTextField field;
  private JPanel gui;
  
  public View(final PrintWriter out, final BufferedReader in) throws IOException {
  
      button = new JButton("Send");
      field = new JTextField();
  
      gui = new JPanel(new GridLayout(1, 0, 10, 10));
  
      gui.add(button);
      gui.add(field);
  
      add(gui);
  
      button.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent arg0) {
              out.println(field.getText());
  
              try {
                  System.out.println(in.readLine());
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      });
  }
  }
  </code></pre>
  
  <p>Here is the server:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class SimpleServer extends Thread {
  
  public static final int PORT = 8100;
  private static ServerSocket serverSocket = null;
  
  private Socket clientSocket = null;
  
  public void run() {
      String receive, answer;
      try {
          BufferedReader in = new BufferedReader(new InputStreamReader(
                  clientSocket.getInputStream()));
  
          PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
  
          receive = in.readLine();
          System.out.println("[server]" +  receive);
          answer = "hello " + receive;
          out.println(answer);
          out.flush();
  
      } catch (IOException e) {
          System.err.println("IO error \n" + e);
      } finally {
          try {
              clientSocket.close();
          } catch (IOException e) {
              System.err.println("Close socket error \n" + e);
          }
  
      }
  }
  
  public SimpleServer() throws IOException {
  
      while (true) {
          serverSocket = new ServerSocket(PORT);
          try {
              clientSocket = serverSocket.accept();
  
              new Thread(this).start();
  
          } finally {
              serverSocket.close();
          }
      }
  }
  
  public static void main(String[] args) throws IOException {
      SimpleServer server = new SimpleServer();
  }
  }
  </code></pre>