title: Java writing to closed output stream not throwing ioexception
body: <p>Here is the flow of my client/server.</p>
  
  <p>Socket is created in main thread.</p>
  
  <ol>
  <li>Socket passes to Thread 1.</li>
  <li>Client sends data to server</li>
  <li>Server responds to client</li>
  <li>Server closes input stream, output stream, and socket by invoking <code>close()</code></li>
  <li>Socket returned to main thread, and then passed to Thread 2.</li>
  <li>Client writes data to server - no exception, no errors, server gets no data</li>
  <li>Client attempts to read data - no exceptions, no errors</li>
  </ol>
  
  <p>How can I detect the problem that socket was closed?</p>
  
  <pre><code>try {
          os = new DataOutputStream(this.socket.getOutputStream());
          os.write(data);
          os.flush();
          System.out.println("Written " + data.length + " bytes");
      } catch (IOException e) {
          System.out.println("Client failed to write to stream");
          System.out.println(e.getMessage());
          e.printStackTrace();
      }
  </code></pre>
  
  <p>The exception is never thrown. It says <code>Written 60 bytes</code>. Any ideas?</p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>Here is the way that I read the response. I wait for data, read first 4 bytes (which gives the the length of response), and keep reading until I read the specified length. This loop never ends because no data ever comes in.</p>
  
  <pre><code>is = new DataInputStream(this.socket.getInputStream());
      while(true){
          while(is.available() &gt; 0){
              bos.write(is.read());
          }
          if(contentLength == 0 &amp;&amp; bos.size() &gt; 3){
              byte[] bytes = bos.toByteArray();
              byte[] size = Arrays.copyOf(bytes, 4);
              contentLength = ByteBuffer.wrap(size).getInt();
          }
          if(bos.size() - 4 &lt; contentLength){
              continue;
          }
          break;
      }
  </code></pre>