title: Connection reset error when reading data from client java
body: <p>I am developing Client-Server App using Java Socket. My Application Has Server which will listen on Port </p>
  
  <ul>
  <li>Client will connect to that port</li>
  <li>Receive Data From Client</li>
  <li>Send Data To Client</li>
  </ul>
  
  <p>Part of My Code</p>
  
  <pre><code>public void run() {
      System.out.println("Got a client !");
      try {
          // Get Data From Client
          int red = -1;
          byte[] buffer = new byte[5 * 1024]; // a read buffer of 5KiB
          byte[] redData;
          StringBuilder clientData = new StringBuilder();
          String redDataText;
          while ((red = clientSocket.getInputStream().read(buffer)) &gt; -1) {
  
  /* Get Data From Client Here Code Hidden */
  
              System.out.println("Data From Client :"
                      + clientData.toString());
  
              OutputStream out = clientSocket.getOutputStream();
              DataOutputStream dos = new DataOutputStream(out);
  
              String sDataToClient = "TEST DATA TO SEND IN BYTE ARRAY";
  
              byte[] b = sDataToClient.getBytes("UTF-8");
  
              byte[] bClientSend = new byte[b.length + 2];
  
              bClientSend[0] = (byte) 1;
              bClientSend[1] = (byte) 79;
  
              System.arraycopy(b, 0, bClientSend, 2, b.length);
  
              dos.write(bClientSend);
              System.out.println(Arrays.toString(bClientSend));
  
          }
          clientSocket.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p><strong>I Get <code>java.net.SocketException: Connection reset</code> after Data SENT to Client at following line</strong></p>
  
  <pre><code>while ((red = clientSocket.getInputStream().read(buffer)) &gt; -1) {
  </code></pre>
  
  <p><strong>I am able to see Array Contents of <code>System.out.println(Arrays.toString(bClientSend));</code> Then error occurs</strong></p>