title: How to replicate a Connection Reset by peer?
body: <p>I have a server that has the following printed in the log</p>
  <pre><code>Caused by: java.io.IOException: Connection reset by peer
          at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
          at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
          at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
          at sun.nio.ch.IOUtil.write(IOUtil.java:65)
          at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
          at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:134)
          at org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:101)
          at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:157)
          at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1238)
          at org.apache.tomcat.util.net.SocketWrapperBase.doWrite(SocketWrapperBase.java:670)
          at org.apache.tomcat.util.net.SocketWrapperBase.flushBlocking(SocketWrapperBase.java:607)
          at org.apache.tomcat.util.net.SocketWrapperBase.flush(SocketWrapperBase.java:597)
          at org.apache.coyote.http11.Http11OutputBuffer.flushBuffer(Http11OutputBuffer.java:519)
          at org.apache.coyote.http11.Http11OutputBuffer.flush(Http11OutputBuffer.java:260)
          at org.apache.coyote.http11.Http11Processor.flush(Http11Processor.java:1494)
          at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:279)
          at org.apache.coyote.Response.action(Response.java:168)
          at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:317)
  </code></pre>
  <p>I know that the client closed the Connection for some reason, but still the server is writing data. And at this time, the client will return the reset flag &quot;RST&quot;, and the server's log will get the <code>Connection reset by peer</code></p>
  <p>To replicate it, I made the server sleep for few seconds and also created a client as such:</p>
  <pre><code> private void executePostMethod() throws IOException, InterruptedException {
          Map&lt;String, String&gt; responseMap = new HashMap&lt;&gt;();
          String data = &quot;{\&quot;id\&quot;:\&quot;XYZ123\&quot;,\&quot;name\&quot;:\&quot;John Doe\&quot;,\&quot;accountNumber\&quot;:\&quot;ABC123\&quot;}&quot;;
          URL urlObject = new URL(&quot;\&quot;http://localhost/v1/name/validate\&quot;&quot;);
          System.out.println(&quot;Creating Connection&quot;);
          Socket socket = new Socket(InetAddress.getByName(urlObject.getHost()), 7080);
          System.out.println(&quot;Connection Established&quot;);
  
          PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
          printWriter.println(&quot;POST &quot; + urlObject.getFile() + &quot; HTTP/1.0&quot;);
          printWriter.println(&quot;Host: &quot; + urlObject.getHost());
          printWriter.println(&quot;Content-Length: &quot; + data.length());
          printWriter.println(&quot;Content-Type: &quot; + &quot;application/json&quot;);
          printWriter.println();   //Writing an empty line just to notify the server the header ends here
          // and next thing written will the data/content
          printWriter.println(data);
          printWriter.println();
          printWriter.flush();
          Thread.sleep(5000);
          socket.setSoLinger(true, 0);
          socket.close();
      }
  </code></pre>
  <p>Before the server sends the response, the close the socket and the line <code>socket.setSoLinger(true, 0);</code> I believe, sends the RST packet. But I get the <code>org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe</code>
  error instead of <code>Connection reset by peer</code>.</p>
  <p>How to reproduce <code>java.io.IOException: Connection reset by peer</code> ?</p>
  <p><strong>UPDATE</strong></p>
  <p>I analyzed the TCP connection and this is what I got. The server receives the RST packet before it tries to send the data to the client</p>
  <p><a href="https://i.stack.imgur.com/JFnoV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JFnoV.png" alt="enter image description here" /></a></p>
  <p>Now from what I know, if the client sends RST packet to the server, the server gets <code>Connection reset by peer</code> , all I get it <code>Broken pipe</code>.</p>