How to fix java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer? Example

You might have seen the java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer error while working with Java NIO based server which is using SocketChannel instead of InputStream for reading data from the network. In general, this can come at both client and server end of a client-server Java application which is using TCP/IP to connect each other. Though this exception usually comes at the server end and the client was complaining that they are not able to connect. 

From the error message, it's quite clear that before the client or server could read the data from SocketChannel, another party has disconnected the session. Let's see the root cause of the problem and how to solve java.net.SocketException: Failed to read from SocketChannel: Connection reset by a peer in Java application.


Cause of java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer

As indicated by error, java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer, Server is not able to read from the SocketChannel opened to the client because peer (another party, a client in this case) has already closed the connection. 

Of course, this assumption is true when you see the error in a server log file. It could also be due to the network issue, which is causing an abrupt drop of connection at the client end.



BTW, Failed to read from SocketChannel: Connection reset by peer error is different from java.net.SocketException: Connection refused", which comes when another end like server is down or host/port combination is incorrect.

If there is no network issue, check your process. If the server sends something to the client which it doesn't like and close the connection in protest, but the Server continues to write on the connection then the TCP stack at the peer end will issue RST which will cause this error on the server-side.

Sometimes this can also be due to heavy load causing the Server to queue the message and before it can read the message is got timed out at the client end. So you can also check server health and log for excessive load causing this error.

This error is common on Java server applications running on Linux or Solaris operating system, where a large number of clients connected to the server and waiting in login queue for authentication and authorization, a delay of a couple of seconds would be enough to trigger timeout at the client end, especially if the client is also a Java application.

On a similar note, if you have not read any book on Java NIO or Socket programming, then you should read at least one this year, TCP/IP Sockets in Java, Second Edition: Practical Guide for Programmers good be a great one.

How to fix java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer




Solution of java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer:


Here are a couple of things you can try to fix this error in your Java application, but remember to try to find out the root cause because the error could just be a manifestation of something else like if your server depends upon some other process and that was stuck or down.

1) Sometimes bouncing client works, so if you can bounce the client then just bounce and reconnect to the server.

2) If the issue persists then check what is received from the server, you can use Wireshark to capture TCP messages. If the Server is sending something garbage then check on the Server side.

3) Check if there is excessive traffic or load in your server. If yes then load balance the load.

4) Check if your server depends upon some other application or server and waiting for its response, triggers the queuing of client messages. It's possible that another server is stuck due to common issues like either its own or stuck due to space issue or crashed by throwing java.lang.OutOfMemoryError: Java Heap Space. You need to check the server log to find out the root cause.

5) If the issue persists, just take the help of your network team or senior Java developer, or team lead.


In short, there is nothing much you can do other than bouncing the server. In most cases, that will resolve this problem but if it is coming consistently and caused by an upgrade to messages, protocol or operating system, then it's worth checking further. You can check what the server is sending to find out if the Server is fine and it's a network or Infra issue.

As I said earlier, sometimes this can also be due to heavy load causing the Server to queue the message and before it can read the message is got timed out at the client end. So you can also check server health and log for excessive load causing this error.

No comments :

Post a Comment