title: BufferedReader readLine method hangs and block program
body: <p>I'm trying to learn sockets using Java and I sucessfully sent data to a ServerSocket running in my own machine. When I try to read from this socket using readline (so I can just echo my own sent message) my program hangs and won't return.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>public static void main(String[] args) throws UnknownHostException, IOException {
  
      TCPClient cli = new TCPClient("127.0.0.1", "15000");
      try {
          cli.ostream.writeUTF("Teste");
          String echo = cli.istream.readLine(); //it hangs in this line
          System.out.println(echo);
      }
  </code></pre>
  
  <p>TCPClient is a class I defined so I can test my program on a simpler interface before using swing on my homwework. here's the code:</p>
  
  <pre><code>public class TCPClient {
  
  public DataOutputStream ostream = null;
  public BufferedReader istream = null;
  
  public TCPClient(String host, String port) throws UnknownHostException {
      InetAddress ip = InetAddress.getByName(host);
  
      try {
          Socket socket = new Socket(host, Integer.parseInt(port));
  
          ostream = new DataOutputStream(socket.getOutputStream());
          istream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
  
      } catch (IOException ex) {
          Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
      }
  
  }
  </code></pre>
  
  <p>My server is pretty simple. After the connection is estabilished, it enters in this loop and stays here until I close the client (because of the infinite loop). Afterwards, some exception handling returns it to the point before the connection started.</p>
  
  <pre><code>            while(true){
                  String msg = istream.readLine();
                  System.out.println("Arrived on server: " + msg); //just works on debug
                  ostream.writeUTF("ACK: " + msg);
                  ostream.flush();
              }
  </code></pre>
  
  <p>I don't see what am I missing.</p>
  
  <p>PS: the wierd stuff is that if I debug the server, I can see the message arriving there (I can print it, for example), but this isn't possible if I just run this code. Does this have some concurrency relation I'm overlooking?</p>
  
  <p>thx</p>