title: SSLSocket client doesn't throw exception if server is shutdown when writing to OutputStream
body: <p><strong>EDIT:</strong> </p>
  
  <p>I have changed the code so that the integrity of the file is checked after the transfer.</p>
  
  <p>But now when I put a breakpoint in the client at <code>dos.write(buffer, 0, count)</code>, kill the server, and then resume client code execution, it hangs at <code>serverMD5[i] = dataInputStream.readByte()</code> indefinitely.</p>
  
  <p>Even though the user now knows that the transfer was not successful (the application hangs and needs to be restarted), once again this is not doing what I expected it to do (throw an IOException).</p>
  
  <p><strong>Original post with changed code:</strong></p>
  
  <p>I have created an android client that connects to a server using SSLSocket and sends some data.
  Here is the relevant client and server code</p>
  
  <p>Client:</p>
  
  <pre><code>try {
    SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket();
    sslsocket.connect(new InetSocketAddress(SERVER_IP, UPLOAD_PORT), 2000);
  
    OutputStream outputStream = sslsocket.getOutputStream();
  
    DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
    dataOutputStream.writeInt(DEVICE_ID);
    dataOutputStream.writeLong(FILE_LENGTH);
  
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestOutputStream dos = new DigestOutputStream(outputStream, md);
  
    InputStream readingsInputStream = new FileInputStream(FILE_NAME);
  
    int count;
    byte[] buffer = new byte[10 * 1024];
    while ((count = readingsInputStream.read(buffer)) &gt; 0) {
      dos.write(buffer, 0, count);
    }
    readingsInputStream.close();
  
    byte[] md5 = md.digest();
    byte[] serverMD5 = new byte[16];
  
    DataInputStream dataInputStream = new DataInputStream(sslsocket.getInputStream());
  
    for (int i = 0;i&lt;16;i++) {
      serverMD5[i] = dataInputStream.readByte();
      if (md5[i] != serverMD5[i]) throw new Exception("MD5 mismatch");
    }
  
    sslsocket.close();
  } catch (Exception e) {
    ...
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>try {
    SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
  
    InputStream inputStream = sslSocket.getInputStream();
  
    DataInputStream dataInputStream = new DataInputStream(inputStream);
    int deviceID = dataInputStream.readInt();
    long fileLength = dataInputStream.readLong();
  
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream dis = new DigestInputStream(inputStream, md);
  
    OutputStream readingsOutputStream = new FileOutputStream("Device"+deviceID+".txt", false);
  
    int count;
    byte[] buffer = new byte[1];
    do {
      count = dis.read(buffer);
      readingsOutputStream.write(buffer, 0, count);
      fileLength -= count;
    } while (fileLength &gt; 0);
  
    readingsOutputStream.close();
  
    byte[] md5 = md.digest();
  
    DataOutputStream md5OutputStream = new DataOutputStream(sslSocket.getOutputStream());
    for (int i = 0;i&lt;16;i++) md5OutputStream.writeByte(md5[i]);
  
    sslSocket.close();
  } catch (Exception e) {
    ...
  }
  </code></pre>
  
  <p>Normally this all works as expected but the problem occurs when I put a breakpoint in the client at the line <code>dos.write(buffer, 0, count)</code> and then kill the server upon reaching the breakpoint.</p>
  
  <p>After continuing code execution on the client, it doesn't throw an exception and just goes through the rest of this code block, leading me to believe that the file was successfully written to the server.</p>
  
  <p>Of course this is not the case since the server was shutdown before the OutputStream was written to. This results in an empty <code>DeviceX.txt</code> (X being the number of the device) file on the server.</p>
  
  <p>This is a big problem since the user might think that the data was successfully transferred and delete it from the device (the data that is sent gets deleted at some point after due to the nature of the application).</p>
  
  <p>Since I have managed to produce this bug I figure it is a possibility that it will happen in a real-world scenario as well. This is my first time working with sockets and I am at a loss as to what to do to fix this issue.</p>
  
  <p>Also if anyone notices anything else that could go wrong with this code block (another scenario where the result is not as expected but an exception isn't thrown) please let me know.</p>