title: "java.net.SocketException: Connection reset by peer: socket write error" How can I solve it
body: <p>I have a problem with my senior project. My objective is to read and modify HTTP response packet. I have a problem about <code>java.net.SocketException</code>. Could you help me, please?
  Exception at Responsethread</p>
  <pre class="lang-none prettyprint-override"><code>java.net.SocketException: Connection reset by peer: socket write error
      at java.net.SocketOutputStream.socketWrite0(Native Method)
      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:147)
      at helper.ResponseThread.run(ResponseThread.java:163)
  BUILD STOPPED (total time: 4 minutes 3 seconds)
  </code></pre>
  <p>Here is my code</p>
  <p>Helper.java</p>
  <pre class="lang-java prettyprint-override"><code>package helper;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.InetAddress;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Helper {
      public void proc() {
          try {
              
              ServerSocket servsock = new ServerSocket(80);
              String hostname=&quot;www.hungeng.besaba.com&quot;;
              InetAddress ip=InetAddress.getByName(hostname);
              //int serverPort=3456;
              //Socket clientSocket =new Socket(ip,serverPort);
             //String ipAddr=&quot;31.170.164.70&quot;; //http://hungeng.besaba.com/
  
              while (true) {
                
                  Socket sockC = servsock.accept();
  
                  //get input stream of the Socket(sockC) from Client 
                  InputStream inC = sockC.getInputStream();
                  //get output stream of the Socket(sockC) to Client
                  OutputStream outC = sockC.getOutputStream();
  
                  //Connect to the specified server(ipAddr) at the 80 port. 
                  Socket sockS = new Socket(ip, 80);
  
                  //get input stream of the Socket(sockS) from server(ipAddr)
                  InputStream inS = sockS.getInputStream();
                  //get output stream of the Socket(sockS) to server(ipAddr)
                  OutputStream outS = sockS.getOutputStream();
  
                  //Create Thread for sending The Request Message from Client 
                  RequestThread request = new RequestThread();
                  //Create Thread for sending The Response Message to Client
                  ResponseThread response = new ResponseThread();
  
                  // match a Request Thread with a Response Thread
                  request.server = response;
                  response.client = request;
                  request.is = inC;
                  request.os = outC;
                  response.is = inS;
                  response.os = outS;
                  
                  request.start();
                  response.start();
              }
          } catch (Exception x) {
              x.printStackTrace();
          }
  
      }
      public static void main(String[] args) {
          // TODO code application logic here
           new Helper().proc();
      }
  }
  </code></pre>
  <p>RequestThread.java</p>
  <pre class="lang-java prettyprint-override"><code>package helper;
  
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  public class RequestThread extends Thread {
      InputStream is;
      OutputStream os;
      ResponseThread server;
      byte[] buf = new byte[1024];
      int bytesRead;
  
      public void run() {
          try {
             int i=0;
             
             while ((bytesRead = is.read(buf)) !=-1) {
                 server.os.write(buf,0,bytesRead);
                 server.os.flush();
              }
           
          } catch (Exception x) {
              x.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>ResponseThread.java</p>
  <pre class="lang-java prettyprint-override"><code>package helper;
  
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  public class ResponseThread extends Thread {
      int num;
      String content = &quot;&quot;;
      InputStream is;
      OutputStream os;
      RequestThread client;
      byte[] buf = new byte[20000];
      int bytesRead = 0;
      String endMark = &quot;\r\n\r\n&quot;;
  
      public void run() {
          try {
              String msg = &quot;&quot;;
  
              while ((bytesRead = is.read(buf)) != -1) {
  
                  msg += new String(buf, 0, bytesRead);
                //  System.out.println(&quot;message&quot;);
                  //System.out.println(msg);
                 // System.out.println(&quot;---------&quot;);
                  //client.os.write(buf, 0, bytesRead);
                  int eHeader = msg.indexOf(&quot;\r\n\r\n&quot;);
                  String header = msg.substring(0, eHeader + &quot;\r\n\r\n&quot;.length());
  
                  // System.out.println(&quot;header&quot;);
                  //System.out.println(header);
                  //System.out.println(&quot;-----------&quot;);
                  int sConLength = header.indexOf(&quot;Content-Length: &quot;);
                  if (sConLength != -1) {
                      //have content length  
                      String temp = header.substring(sConLength);
                      int eConLength = temp.indexOf(&quot;\r\n&quot;);
                      int cl = Integer.parseInt(temp.substring(&quot;Content-Length: &quot;.length(), eConLength));
                      String uConlen = header.substring(0, sConLength + &quot;Content-Length: &quot;.length());
                      System.out.println(&quot;uconlen &quot;);
                      System.out.println(uConlen);
                      System.out.println(&quot;--------&quot;);
  
                      String lConlen = temp.substring(eConLength + &quot;\r\n&quot;.length());
                      System.out.println(&quot;lconlen &quot;);
                      System.out.println(lConlen);
                      System.out.println(&quot;-----------------&quot;);
                      int sHtml = msg.toLowerCase().indexOf(&quot;&lt;html&gt;&quot;);
                      int eHtml = msg.toLowerCase().indexOf(&quot;&lt;/html&gt;&quot;);
                      if ((sHtml != -1) &amp;&amp; (eHtml != -1)) {
                          //has Html content
                          System.out.println(&quot;:::Have Html content:::&quot;);
                          int sForm = msg.toLowerCase().indexOf(&quot;&lt;form&quot;);
                          int eForm = msg.toLowerCase().indexOf(&quot;&lt;/form&gt;&quot;);
                          if ((sForm != -1) &amp;&amp; (eForm != -1)) {
                              //have form
                              System.out.println(&quot;:::Have form:::&quot;);
                              String form = msg.substring(sForm, eForm + &quot;&lt;/form&gt;&quot;.length());
                              String uForm = msg.substring(eHeader + &quot;\r\n\r\n&quot;.length(), sForm);
                              String lForm = msg.substring(eForm + &quot;&lt;/form&gt;&quot;.length());
                              String p = &quot;&lt;p id=\&quot;demo\&quot;&gt;&lt;/p&gt;&quot;;
                              String bt = &quot;&lt;button type=\&quot;button\&quot; onclick=\&quot;fill_in(name)\&quot;&gt;Fill In&lt;/button&gt;\n&quot;;
                              String[] data = {&quot;Natta santawalim&quot;, &quot;110033333333&quot;, &quot;adressssss&quot;};
                              String sc = &quot;&lt;script&gt;\n &quot;;
                              sc += &quot;function fill_in(name) \n&quot;;
                              sc += &quot;{\n&quot;;
                              sc += &quot;document.getElementById(\&quot;txtUsername\&quot;).value=\'&quot;;
                              sc += data[0];
                              sc += &quot;\'; \n&quot;;
                              sc += &quot;document.getElementById(\&quot;txtPassword\&quot;).value=\'&quot;;
                              sc += data[1];
                              sc += &quot;\'; \n&quot;;
                              sc += &quot;document.getElementById(\&quot;txtName\&quot;).value=\'&quot;;
                              sc += data[2];
                              sc += &quot;\'; \n&quot;;
                              sc += &quot;}\n&quot;;
                              sc += &quot;&lt;/script&gt;\n&quot;;
  
                              // client.os.write(result.getBytes());
                              cl += bt.length() + p.length() + sc.length();
                              client.os.write(uConlen.getBytes());
                              String l = Integer.toString(cl) + &quot;\r\n&quot;;
                              client.os.write(l.getBytes());
                              client.os.write(lConlen.getBytes());
                              client.os.write(uForm.getBytes());
                              client.os.write(form.getBytes());
                              client.os.write(bt.getBytes());
                              client.os.write(sc.getBytes());
                              client.os.write(p.getBytes());
                              client.os.write(lForm.getBytes());
  
                              //                       System.out.println(&quot;byte &quot;+);
                              //System.out.println(&quot;numofsent byr&quot;+s);
                          } else {
                              //don't have form
                              System.out.println(&quot;:::Dont Have form:::&quot;);
                              //client.os.write(buf, 0, bytesRead);
                              String packet = msg.substring(eHeader + &quot;\r\n\r\n&quot;.length());
                              client.os.write(header.getBytes());
                              client.os.write(packet.getBytes());
  
                              client.os.flush();
                          }
  
                      } else {
                          // don't have Html content
                          System.out.println(&quot;:::Dont Have Html content:::&quot;);
                          client.os.write(buf, 0, bytesRead);
                          //client.os.flush();
                          //  num+=bytesRead;
                          //System.out.println(&quot;num &quot;+num);
                      }
                  } else {
                      //don't have content length,transfer-encoding: chunk
                      System.out.println(&quot;chunk&quot;);
                      //client.os.write(buf, 0, bytesRead);
                      // System.out.println(&quot;message&quot;);
                      //System.out.println(msg);
                      int fChunk = header.indexOf(&quot;Transfer-Encoding: chunked&quot;);
                  //    String m = msg.substring(eHeader + &quot;\r\n\r\n&quot;.length());
                      if (fChunk != -1) {
                          //chunk
  
                          int sHtml = msg.toLowerCase().indexOf(&quot;&lt;html&gt;&quot;);
                          int eHtml = msg.toLowerCase().indexOf(&quot;&lt;/html&gt;&quot;);
                          if ((sHtml != -1) &amp;&amp; (eHtml != -1)) {
                              //have html
                              String packet = msg.substring(eHeader + &quot;\r\n\r\n&quot;.length());
                              
                              String[] chunkPt = packet.split(&quot;\r\n&quot;);
                              //  System.out.println(&quot;=====chunk=========== &quot;);
                              
                              client.os.write(header.getBytes());
                              for (int i = 0; i &lt; (chunkPt.length - 1); i++) {
                              
                                int fForm=chunkPt[i].toLowerCase().indexOf(&quot;&lt;/form&gt;&quot;);
                                if(fForm!=-1){
                                     String bt = &quot;&lt;button type=\&quot;button\&quot; onclick=\&quot;fill_in(name)\&quot;&gt;Fill In&lt;/button&gt;\n&quot;;
                                     int btSizeDec=bt.length();
                                     int cSizeDec=Integer.parseInt(chunkPt[i-1],16);
                                     int totalSizeDec=btSizeDec+cSizeDec;
                                     String totalSizeHex=Integer.toHexString(totalSizeDec);
                                     String h=chunkPt[i].substring(0,fForm);
                                     String t=chunkPt[i].substring(fForm);
                                     chunkPt[i]=h+bt+t;
                                     chunkPt[i-1]=totalSizeHex;
                                     System.out.println(&quot;chunkEmbedded&quot;);
                                     System.out.println(chunkPt[i]);
                                     
                   
                                }
                                  client.os.write((chunkPt[i]+&quot;\r\n&quot;).getBytes());//Error occured here
                               
  
                              }
                              client.os.write((chunkPt[chunkPt.length - 1] + &quot;\r\n\r\n&quot;).getBytes());
                              
                          } else {
                              System.out.println(&quot;dont hav html&quot;);
                              //dont have html
                          }
  
                      } else {
                          //dont chunk
                          System.out.println(&quot;dont chunk&quot;);
                      }
                  }
  
              }
              client.os.flush();
          } catch (Exception x) {
              x.printStackTrace();
          }
      }
  }
  </code></pre>