Network unavailable java net socketexception software caused connection abort recv failed

java.net.SocketException: Software caused connection abort: recv failed

I am using HttpClient to connect to a URL and i’m getting a socket exception. here is my code and output.

private String callWebService(String xml)

String response = new String();

HttpClient client = new HttpClient();

NameValuePair[] data = new NameValuePair(«method», «synchronousInvoke»),
new NameValuePair(«formxml», xml)
>;

try int status = client.executeMethod(get);

//if(status == HttpStatus.SC_OK)
// System.err.println(«Method completed: » + get.getStatusLine());
//else if(status != HttpStatus.SC_OK)
// System.err.println(«Method failed: » + get.getStatusLine());

// get the response
//BufferedReader br = new BufferedReader(new InputStreamReader(get.getR esponseBod yAsStream( )));
//String line = new String();
//while((line = br.readLine()) != null)
// response += line + «\n»;
//br.close();
//System.out.println(«Retr ieved response. «);

System.out.println(«Except ion: » + e.getMessage());
e.printStackTrace();
>

jvm 1 | Jul 26, 2006 6:44:49 PM org.apache.commons.httpcli ent.HttpMe thodDirect or executeWithRetry
jvm 1 | INFO: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: socket write error
jvm 1 | Jul 26, 2006 6:44:49 PM org.apache.commons.httpcli ent.HttpMe thodDirect or executeWithRetry
jvm 1 | INFO: Retrying request
jvm 1 | Exception: Software caused connection abort: recv failed
jvm 1 | java.net.SocketException: Software caused connection abort: recv failed
jvm 1 | at java.net.SocketInputStream .socketRea d0(Native Method)
jvm 1 | at java.net.SocketInputStream .read(Unkn own Source)
jvm 1 | at java.io.BufferedInputStrea m.fill(Unk nown Source)
jvm 1 | at java.io.BufferedInputStrea m.read(Unk nown Source)
jvm 1 | at org.apache.commons.httpcli ent.HttpPa rser.readR awLine(Htt pParser.ja va:77)
jvm 1 | at org.apache.commons.httpcli ent.HttpPa rser.readL ine(HttpPa rser.java: 105)
jvm 1 | at org.apache.commons.httpcli ent.HttpCo nnection.r eadLine(Ht tpConnecti on.java:11 15)
jvm 1 | at org.apache.commons.httpcli ent.HttpMe thodBase.r eadStatusL ine(HttpMe thodBase.j ava:1832)
jvm 1 | at org.apache.commons.httpcli ent.HttpMe thodBase.r eadRespons e(HttpMeth odBase.jav a:1590)
jvm 1 | at org.apache.commons.httpcli ent.HttpMe thodBase.e xecute(Htt pMethodBas e.java:995 )
jvm 1 | at org.apache.commons.httpcli ent.HttpMe thodDirect or.execute WithRetry( HttpMethod Director.j ava:397)
jvm 1 | at org.apache.commons.httpcli ent.HttpMe thodDirect or.execute Method(Htt pMethodDir ector.java :170)
jvm 1 | at org.apache.commons.httpcli ent.HttpCl ient.execu teMethod(H ttpClient. java:396)
jvm 1 | at org.apache.commons.httpcli ent.HttpCl ient.execu teMethod(H ttpClient. java:324)
jvm 1 | at com.fourpoint.poller.Start .callWebSe rvice(Star t.java:118 )
jvm 1 | at com.fourpoint.poller.Start .(St art.java:4 7)
jvm 1 | at com.fourpoint.poller.Start .main(Star t.java:245 )
jvm 1 | at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
jvm 1 | at sun.reflect.NativeMethodAc cessorImpl .invoke(Un known Source)
jvm 1 | at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Unknown Source)
jvm 1 | at java.lang.reflect.Method.i nvoke(Unkn own Source)
jvm 1 | at org.tanukisoftware.wrapper .WrapperSi mpleApp.ru n(WrapperS impleApp.j ava:292)
jvm 1 | at java.lang.Thread.run(Unkno wn Source)
jvm 1 | web service call responded with:

Источник

How to fix java.net.SocketException: Software caused connection abort: recv failed

Out of many client servers related socket errors here is one more interesting socket related error from the Java program, «java.net.SocketException: Software caused connection abort: recv failed». The key point in this error message is «abort» and «recv», which means is someone (client or server) is trying to read from a closed connection. This error usually comes at the client socket end, when the server closed the connection before the client has read the response, but, in general, it can come to an end of the TCP socket, so you must check the log files for both clients and server to find out who is complaining. If the Server is complaining then it’s fine and the client has closed the TCP connection may be due to timeout or any RuntimeException at the client end.

Читайте также:  Импорт стиля

If the client’s log file contains this error it means your client is fine and it’s the server who is closing the connection prematurely. This generally happens when the Server is waiting for the response from some other process e.g. another and that is overloaded and in the meantime, the client request has timed out at the Server end.

Many Java developers encounter «java.net.SocketException: Software caused connection abort: recv failed» error at different places e.g. it can come when your Java program is connected to MySQL database and left running idle for many hours. You may get this while using products written in Java like Tomcat, Apache Cassandra or Apache Axis 2. It can also be related to database problems. Just check if Server is healthy and why it’s closing the connection.

Scenario 1: java.net.SocketException: Software caused connection abort: recv failed

  1. Client sends a request req#1
  2. Server reads the first request req#1, processes, writes a response
  3. Server closes connection
  4. Client sends second request req#2
  5. Client tries to read server response: «java.net.SocketException: Software caused connection abort: recv failed» as shown below:

Solution 1:
Check why your client is sending the second request before reading the response of the first request. Look for any multi-threading angle if the error is intermittent.

How to solve java.net.SocketException: Software caused connection abort: recv failed

Scenario 1: java.net.SocketException: Software caused connection abort: recv failed

I have also seen this error while working with a Java Web application connecting to a database. When you left your application overnight and there is no request to process during the night, in the morning it was throwing «java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed» error.

Читайте также:  Php remove domain extension

The real cause of the problem was that the database timed out the connection when left idle and when the Java application tries to connect to the database using those stale connections it throws java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed error.

Solution 2:
Since we were using the c3po connection pool from Hibernate and enabled connection.autoReconnect , connection.autoReconnectForPools , and connection.is-connection-validation-required set, I was wondering why this issue is coming even if we auto-connect enabled. The real reason was that we have not provided the validationQuery property and JDBC drivers need validation queries to test the connection during the idle period.

You can use queries like «SELECT 1» or «SELECT 1 from DUAL» depending upon which database you are connecting In our case, it was Microsoft SQL SERVER 2008 but this error can happen with any database e.g MySQL or Oracle. Use the second validation query if you are connecting to the Oracle database from Java application using the c3p0 connection pool.

If you are not using c3p0 but using Apache DBCP connection pool i.e. using org.apache.commons.dbcp.BasicDataSource in your Spring config file then uses relevant property so that the JDBC connection pool can automatically test the pooled database connection.

That’s all about java.net.SocketException: Software caused connection abort: recv failed». You see the root cause is that one party, usually the server has closed the connection but the client is trying to read from that connection. I understand every situation is different and it may require different solutions but knowing this fundamental will help you to quickly troubleshoot your issue.

  • java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer [fix]
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet [fix]
  • 2 ways to solve Unsupported major.minor version 51.0 error in Java [solutions]
  • How to solve java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver [solution]
  • java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [solution]
  • How to solve java.lang.UnsatisfiedLinkError: no ocijdbc11 in Java [solution]
  • java.lang.ClassNotFoundException: org.postgresql.Driver [fix]
  • Fixing Unsupported major.minor version 52.0 Error in Java [solution]
  • java.io.IOException: Map failed and java.lang.OutOfMemoryError: Map failed [fix]
  • Exception in thread «main» java.lang.ExceptionInInitializerError in Java Program [fix]
  • Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: 1 [solution]
  • org.hibernate.MappingException: Unknown entity Exception in Java [solution]
  • java.net.BindException: Cannot assign requested address: JVM_Bind [fix]
  • java.net.SocketException: Too many files open java.io.IOException [solution]

Источник

java.net.SocketException: Software Caused Connection Abort: Recv Failed in Java

java.net.SocketException: Software Caused Connection Abort: Recv Failed in Java

This tutorial demonstrates how to solve the following error in Java:

java.net.SocketException: Software caused connection abort: recv failed 

Solve the java.net.SocketException: 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.

Читайте также:  Can constructor be public in java

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 java.net.SocketException: Software caused connection abort: recv failed exceptions.

BufferedReader Buffered_Reader; String URI ="https://www.delftstack.com/"; try   URL DEMOURL = new URL(URI);  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()) )   Result_StringBuilder.append(DemoBuffer); > Buffered_Reader.close(); 

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 ="https://www.delftstack.com/"; HttpClient Demo_Http_Client = HttpClients.custom()  .setConnectionTimeToLive(10, TimeUnit.SECONDS)  .setMaxConnTotal(200).setMaxConnPerRoute(200)  .setDefaultRequestConfig(RequestConfig.custom()  .setSocketTimeout(15000).setConnectTimeout(2500).build())  .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))  .build(); // 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.

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.

Related Article — Java Error

Источник

Оцените статью