How does java net SocketException Connection reset happen

+2 votes

We are seeing frequent java.net.SocketException: Connection reset errors in our logs for a component that calls a third party Web service that sends SMS messages.

Our application is written in Java and runs on top of Tomcat 5.5. It was written by contractors who are no longer with us. The current team has no real Java expertise, and we are unsure as to where the Connection reset error is actually coming from, and how to go about debugging.

The issue appears to be completely intermittent, and unrelated to the messages we are attempting to send.

Any suggestions on what the typical causes of this exception might be, and how we might proceed, are welcome.

The whole call stack is included below for completeness.

(com.companyname.mtix.sms is our component)

    java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
        at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
        at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
        at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115)
        at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832)
        at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
        at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
        at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
        at com.companyname.mtix.sms.services.impl.message.SendTextMessage.sendTextMessage(SendTextMessage.java:127)
        at com.companyname.mtix.sms.services.MessageServiceImpl.sendTextMessage(MessageServiceImpl.java:125)
        at com.companyname.mtix.sms.services.remote.MessageServiceRemoteImpl.sendTextMessage(MessageServiceRemoteImpl.java:43)
        at sun.reflect.GeneratedMethodAccessor203.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
        at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
        at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
        at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
        at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
        at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
        at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:453)
        at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
        at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at com.companyname.mtix.sms.http.filters.NoCacheFilter.doFilter(NoCacheFilter.java
Jun 21, 2018 in Java by developer_1
• 3,350 points
253,299 views
Did you get answer to your question? I am facing same issue.
Hello, there are multiple answers already for the problem mentioned above. Take a look at them and let me know if it doesn't work.
No I didn't get answer to my question
This may usually happen coz the server has closed the connection at its end (issue at the server end) or probably an issue with the request that you send to the server. This exception might be hinting towards a probable TCP error that has occured. You might use tools such as Wireshark as suggested by @Rishabh to debug further.
In my case I used httpclient post method. I set request header value as

post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1").

My application was rolled to production and ran successfully for 1 year. On a sudden it stopped working and I received javax.net.ssl.SSLProtocolException: Connection reset

Fix:

I changed post.setRequestHeader("Content-Type", "application/xml")

This resolved my issue.

13 answers to this question.

0 votes

The javadoc for SocketException states that it is

Thrown to indicate that there is an error in the underlying protocol such as a TCP error

In your case it seems that the connection has been closed by the server end of the connection. This could be an issue with the request you are sending or an issue at their end.

To aid debugging you could look at using a tool such as Wireshark to view the actual network packets. Also, is there an alternative client to your Java code that you could use to test the web service? If this was successful it could indicate a bug in the Java code.

As you are using Commons HTTP Client have a look at the Common HTTP Client Logging Guide. This will tell you how to log the request at the HTTP level.

In order to become a good a Java programmer since you are not one, I suggest you join a comprehensive Java certification course now.

answered Jun 21, 2018 by Rishabh
• 3,620 points
0 votes

This error happens on your side and NOT the other side. If the other side reset the connection, then the exception message should say:

java.net.SocketException reset by peer

The cause is the connection inside HttpClient is stale. Check stale connection for SSL does not fix this error. Solution: dump your client and recreate.

answered Nov 5, 2018 by Sushmita
• 6,920 points
0 votes

You should check the closing of the Socket in your client program, that could be a prominent cause of Server side streams to close abruptly.

answered Dec 7, 2018 by Divyanshu
0 votes

You should check with the other whether it deliberately resets the connection. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software.

answered Dec 7, 2018 by testerr
0 votes

Most common issue for this problem occurring is when you close the socket, and then write more data on the output stream. By closing the socket, you told your peer that you are done talking, and it can forget about your connection. When you send more data on that stream anyway, the peer rejects it with an RST to let you know it isn't listening. You should check the following.

answered Dec 7, 2018 by raga
0 votes

When I got this error I found out that the remote host itself "forget" about the TCP connection. This happened when I didn't send data for a long period of time. 

The other reason that might be possible could be that the peer was rebooted and lost its information about active connections. Sending data on one of these defunct connections will cause a RST too.

answered Dec 7, 2018 by ravi
0 votes

SocketTimeoutException- This exception is raised if the configured timeout is exceeded while blocked on a socket operation. The state of the socket itself is not changed when this exception is thrown, but if your exception handler closes the socket, and then tries to write to it, you'll be in a connection reset condition.

answered Dec 7, 2018 by logdumps
+1 vote

Check  the heap on the server side, it could be full, that could be a probable reason for the connection reset.

Increase the memory available to the JVM: problem solved!

answered Dec 7, 2018 by neha
+1 vote

I had faced the same error what I found was that my machine didn't have the permission to connect to the remote server. write() method was working fine, but the read() method was throwing a java.net.SocketException: Connection reset.
I fixed this problem by adding a client SSH key to the remote server.

answered Dec 7, 2018 by dhruva
How to generatge Client SSH key? How to add client SSH key to remote server?
+1 vote
You can use wireshark to view the data properly and then figure out the issue. Generally there are disconnected links that you only know when you run your code.
answered Dec 7, 2018 by tushh
0 votes

Connection reset socket error occurs when the opponent is forcibly terminated without calling close(). This  simply means that a TCP RST was received. TCP RST packet is that the remote side telling you the connection on which the previous TCP packet is sent is not recognized, maybe the connection has closed, maybe the port is not open, and something like these. A reset packet is simply one with no payload and with the RST bit set in the TCP header flags. There are several possible causes.

  • The other end has deliberately reset the connection, in a way which I will not document here. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software.
  • More commonly, it is caused by writing to a connection that the other end has already closed normally. In other words an application protocol error.
  • It can also be caused by closing a socket when there is unread data in the socket receive buffer.

answered Dec 17, 2019 by walemark
• 140 points
0 votes

The java.net.SocketException: Connection reset error usually comes when one of the parties in TCP connection like client or server is trying to read/write data, but other parties abruptly close the connection like it was crashed, stopped or terminated.

The java.net.SocketException: Connection reset error usually comes when one of the parties in TCP connection like client or server is trying to read/write data, but other parties abruptly close the connection like it was crashed, stopped or terminated.

You will also not receive this error if the Client closes the connection using the close() method before sever sends the response. This occurred when Server closed the connection, while the client is still waiting to read data from its InputStream.

 

answered Dec 10, 2020 by Gitika
• 65,770 points
0 votes

If you are a client and getting this error while connecting to the server-side application then you can do the following things:

1. First, check if the Server is running by doing telnet on the host port on which the server runs.
In most cases, you will find that either server is not running or restarted manually or automatically.

2. Check if the server was restarted

3. Check if the server failed over to a different host

4. log the error

5. Report the problem to the server team

That's all about how to deal with the java.net.SocketException: Connection reset Exception in Java




 

answered Dec 10, 2020 by Rajiv
• 8,870 points

Related Questions In Java

0 votes
2 answers

Connection reset : java.net.SocketException

You should check whether the client program is ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,920 points
5,894 views
0 votes
0 answers

java.net.SocketException: Connection reset

I was having an error when I ...READ MORE

May 8, 2022 in Java by Kichu
• 19,040 points
1,017 views
0 votes
1 answer

How does omitting curly braces in Java program, effect the code ?

Using braces makes the code more maintainable ...READ MORE

answered May 14, 2018 in Java by Rishabh
• 3,620 points
3,043 views
0 votes
3 answers

How does the “final” keyword in Java work?

Final is a keyword that is used ...READ MORE

answered Sep 1, 2019 in Java by DEEPAK KUMAR GUPTA
2,454 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
962 views
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,190 points
1,252 views
0 votes
1 answer

How to resolve the java.net.ConnectException in Java?

If you are getting java.net.ConnectionException, it means ...READ MORE

answered Jun 4, 2018 in Java by Akrati
• 3,190 points
2,272 views
+1 vote
1 answer

How can we resolve java.lang.OutOfMemoryError in Java?

Work with smaller batches of HashMap Objects ...READ MORE

answered Jun 5, 2018 in Java by sharth
• 3,370 points
1,010 views
0 votes
1 answer

How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?

If you are aware about what port ...READ MORE

answered Feb 22, 2022 in Java by Aditya
• 7,680 points
5,122 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,966 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP