title: java.net.Socket > outputStream > BufferedOutputStream flush() confirmation
body: <p>is there a way of knowing when or whether the flush() method of a BufferedOutputStream thread has finished successfully? In my case I'm using it for sending a simple string through a java.net.Socket. In the following code, the flush() method is run in parallel with the BufferedReader.read() method and the socket output is immediately blocked by the input read resulting in something that resembles a deadlock. What I would like to do is wait for the output to end, and then start reading the input. </p>
  
  <pre><code>Socket sk = new Socket("192.168.0.112", 3000);
  BufferedOutputStream bo = new BufferedOutputStream(sk.getOutputStream());
  bo.write(message.getBytes());
  bo.flush();
  
  BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
  String line = br.readLine();
  if (line.equals("ack")) {
      System.out.println("ack");
  }
  
  
  sk.close();
  </code></pre>
  
  <p><em>Update</em></p>
  
  <p>ServerSocket:</p>
  
  <pre><code>ServerSocket ss = new ServerSocket(3000);
          System.out.println("server socket open");
          while (true) {
              Socket sk = ss.accept();
              System.out.println("new connection");
              BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
              String line = br.readLine();
              System.out.println("received line: " + line);
              BufferedOutputStream bo = new BufferedOutputStream(sk.getOutputStream());
              bo.write("ack".getBytes()); bo.flush();
              sk.close();
          }
  </code></pre>
  
  <p><em>Update</em>:</p>
  
  <p>@Global Variable - the reason that read was blocking the socket is that it was waiting for the \n, indeed. Using</p>
  
  <pre><code>bo.write("ack\n".getBytes());
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>bo.write("ack".getBytes());
  </code></pre>
  
  <p>made it work.</p>
  
  <p>Regarding the initial question, is there a way of knowing if flush() method has finished successfully, @Stephen C provided the answer: </p>
  
  <blockquote>
    <p>there is no way to know that based on the Socket or OutputStream APIs.
    The normal way to get that sort of assurance is to have the remote
    application send an "reply" in response, and read it in the local
    side.</p>
  </blockquote>
  
  <p>This "reply" is implemented in the code sample and it works.</p>