title: SocketException:Connection reset on every request + Spring Integration
body: <p>I see the below line application log.</p>
  <p>ERROR [TcpNetConnection.java:263] Read exception rclegacy-dev.kkl.com:9103:48416:1a40a36a-8cb6-4a2a-9229-ae4fe5273966 SocketException:Connection reset</p>
  <p>There are no issues with the request-response flows. But I see the above line in the logs every time a request is sent and received. I have implemented Spring integration for the TCP communication and using a AbstractByteArraySerializer for Custom (De)Serialiazation. How can I get rid of this error?</p>
  <p>Here is my SI configuration.</p>
  <pre><code>&lt;int:gateway id=&quot;gw&quot; service-interface=&quot; com.api.MyGateway&quot;
          default-request-channel=&quot;objectOut&quot; /&gt;
  
      &lt;int:channel id=&quot;objectOut&quot; /&gt;
  
      &lt;int-ip:tcp-connection-factory id=&quot;client&quot;
          type=&quot;client&quot; host=&quot;#{configProperties.limitKeyMap['host']}&quot;  port=&quot;#{configProperties.limitKeyMap['port']}&quot; single-use=&quot;false&quot;     so-timeout=&quot;50000000&quot; using-nio=&quot;false&quot; so-keep-alive=&quot;true&quot;
          serializer=&quot;customDSerializer&quot; deserializer=&quot;customDSerializer&quot; /&gt;
      &lt;bean id=&quot;customDSerializer&quot; class=&quot;CustomSerializerDeserializer&quot;&gt;
          &lt;property name=&quot;maxMessageSize&quot; value=&quot;4096&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;int-ip:tcp-outbound-gateway id=&quot;outGateway&quot;
          request-channel=&quot;objectOut&quot; reply-channel=&quot;toSA&quot; connection-factory=&quot;client&quot;
          request-timeout=&quot;100000&quot; reply-timeout=&quot;50000&quot;/&gt;
  
      &lt;int:service-activator input-channel=&quot;toSA&quot;
          ref=&quot;myService&quot; method=&quot;parseResponse&quot;/&gt;
  
      &lt;bean id=&quot;myService&quot; class=&quot;com.api.services.MyService&quot;/&gt;
  
      &lt;int:channel id=&quot;toSA&quot; /&gt;
      &lt;int:channel id=&quot;bytesIn&quot; /&gt;
  </code></pre>
  <p>CustomDeserializer</p>
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer;
  
  
  public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
  
  
      
      Logger streamLogger = LoggerFactory.getLogger(CustomSerializerDeserializer.class);
  
      public CustomSerializerDeserializer() {
      }
      
      @Override
      public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
      
          int length = bytes.length;
          outputStream.write(bytes); 
      }
      
      @Override
      public byte[] deserialize(InputStream inputStream) throws IOException {
          byte[] messageBuffer = null;
          messageBuffer = new byte[2400];
          int bytesRead = read(inputStream, messageBuffer, false);
          return messageBuffer;
      }
      
      protected int read(InputStream inputStream, byte[] buffer, boolean header)
              throws IOException {
          
          int lengthRead = 0;
          int dataLength = buffer.length;
          while (lengthRead &lt; dataLength) {
              
              int len;
              len = inputStream.read(buffer, lengthRead,
                      dataLength - lengthRead);
              if (len &lt; 0 &amp;&amp; header &amp;&amp; lengthRead == 0) {
                  streamLogger.debug(&quot;[read] length&lt;0 ? {}&quot;, len);
                  return len;
              }
              if (len &lt; 0) {
                  throw new IOException(&quot;Stream closed after &quot; + lengthRead + &quot; of &quot; + dataLength);
              }
              lengthRead += len;
              if (this.logger.isDebugEnabled()) {
              }
          }
          return lengthRead;
          
      }
      
  }
  </code></pre>
  <p>Stacktrace example</p>
  <pre><code>04/08 22:06:50:794  INFO [MyService.java:492] Amt|0.0|
  04/08 22:06:50:797  ERROR [TcpNetConnection.java:263] Read exception testlegacy-cert.pip.com:9000:46506:3fdd2dff-5bcd-48bc-b6ca-cb03c1277a65 SocketException:Connection reset
  04/08 22:06:50:802  INFO [MyService.java:500] Date |Wed Apr 08 22:06:00 CDT 2020|
  </code></pre>
  <p>Error got printed in between parsing the TCP response. No other errors or exceptions. Request and response works fine.</p>