How does java net SocketException Connection reset happen

+1 vote

We are seeing frequent 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) Connection reset
        at org.apache.commons.httpclient.HttpParser.readRawLine(
        at org.apache.commons.httpclient.HttpParser.readLine(
        at org.apache.commons.httpclient.HttpConnection.readLine(
        at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(
        at org.apache.commons.httpclient.HttpMethodBase.readResponse(
        at org.apache.commons.httpclient.HttpMethodBase.execute(
        at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(
        at org.apache.commons.httpclient.HttpClient.executeMethod(
        at org.apache.commons.httpclient.HttpClient.executeMethod(
        at sun.reflect.GeneratedMethodAccessor203.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at org.apache.axis.strategies.InvocationStrategy.visit(
        at org.apache.axis.SimpleChain.doVisiting(
        at org.apache.axis.SimpleChain.invoke(
        at org.apache.axis.handlers.soap.SOAPService.invoke(
        at org.apache.axis.server.AxisServer.invoke(
        at org.apache.axis.transport.http.AxisServlet.doPost(
        at javax.servlet.http.HttpServlet.service(
        at org.apache.axis.transport.http.AxisServletBase.service(
        at javax.servlet.http.HttpServlet.service(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at com.companyname.mtix.sms.http.filters.NoCacheFilter.doFilter(
Jun 21, 2018 in Java by developer_1
• 3,310 points
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 Connection reset


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.

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: 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,890 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 6, 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 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 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 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,870 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 Connection reset Exception in Java


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

Related Questions In Java

0 votes
2 answers

Connection reset :

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

answered Sep 6, 2018 in Java by Sushmita
• 6,890 points
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
0 votes
3 answers

How does the “final” keyword in Java work?

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

answered Aug 31, 2019 in Java by DEEPAK KUMAR GUPTA
0 votes
1 answer

How does Java convert integer value into byte?

When an integer value is converted into ...READ MORE

answered Feb 28, 2019 in Java by avantika
• 1,500 points

edited Feb 28, 2019 by Omkar 9,099 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,350 points
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,170 points
0 votes
1 answer

How to resolve the in Java?

If you are getting, it means ...READ MORE

answered Jun 4, 2018 in Java by Akrati
• 3,170 points
+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,350 points
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
• 58,990 points
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,200 points