Software Caused Connection Abort: Recv Failed in Java

Sheeraz Gul Jul 19, 2022 Software Caused Connection Abort: Recv Failed in Java

This tutorial demonstrates how to solve the following error in Java: Software caused connection abort: recv failed

Solve the Software Caused Connection Abort: Recv Failed in Java

The SocketException is a sub-class of IOException, which occurs when we are trying to access a socket. The Software Caused Connection Abort: Recv Failed appears when a network error such as a timeout or the server cannot authenticate the TLS client.

Most of the time, this error occurs when using platforms like Tomcat. This error occurs when the TCP connection is reset while data is still in the buffer, which stops the connection abruptly.

This error is similar to the Connection Reset by Peers exception. Here is an example that can throw the Software caused connection abort: recv failed exceptions.

BufferedReader Buffered_Reader;
String URI ="";
try {
    Buffered_Reader = new BufferedReader(new InputStreamReader(DEMOURL.openStream()));
    } catch( MalformedURLException e ) {
        throw new IOException("Enter the correct URL: " + e);

String DemoBuffer;
StringBuilder Result_StringBuilder = new StringBuilder();
while( null != (DemoBuffer = Buffered_Reader.readLine()) ) {

The code above is trying to access the HTTP client, but the connection is closed abruptly. The error will occur at the line:

Buffered_Reader = new BufferedReader(new InputStreamReader(DEMOURL.openStream()));

This is because DEMOURL.openStream() will not work properly for an HTTP client. Using Apache Commons HttpClient will solve this issue.

The HttpClient will re-open the connection and retry the request. Using the HttpClient library will give us a better understanding of the error messages. The HttpClient has connection pooling, retry mechanism, keep-alive, and many other features.

Here is a sample used for this scenario:

String URI ="";
HttpClient Demo_Http_Client = HttpClients.custom()
            .setConnectionTimeToLive(10, TimeUnit.SECONDS)
            .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
// the httpClient can be re-used as it is thread-safe and pooled.

HttpGet Http_Get_request = new HttpGet(URI);
HttpResponse Http_Response = Demo_Http_Client.execute(Http_Get_request);
Buffered_Reader = new BufferedReader(new InputStreamReader(Http_Response.getEntity().getContent()));
// handle the response.

The code above will handle the client-server operation properly, and if any network error occurs, the HttpClient will retry the process.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Error