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.
By the way, this is my fourth article in the series on how to deal with SocketException and ConnectException in Java. If you haven't read the other articles of this series, here are them
- How to fix java.net.SocketException: Software caused connection abort: recv failed
- How to fix java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer
- Overview of 7 common Socket Exception in Java applications
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.
By the wya, 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.
By the wya, 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.
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.
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.
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.
Other Java troubleshooting articles you may like:
Thanks for reading this troubleshooting guide, if you like this tutorial then please share it with your friends and colleagues too. If you have any doubt or questions then please drop a comment.
- How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error? (hint)
- java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
- java.sql.SQLServerException: The index 58 is out of range - JDBC (solution)
- How to fix 'javac' is not recognized as an internal or external command (solution)
- How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger (solution)
- java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error (solution)
- How to avoid ConcurrentModificationException in Java? (tutorial)
- Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java? (solution)
- java.lang.ClassNotFoundException :org.Springframework.Web.Context.ContextLoaderListener (solution)
- How to connect to MySQL database in Java? (tutorial)
- How to solve "could not create the Java virtual machine" error in Java? (solution)
- Fixing java.lang.UnsupportedClassVersionError Unsupported major.minor version 60.0 (solution)
- How to solve java.lang.OutOfMemoryError: Java Heap Space in Eclipse, Tomcat? (solution)
- How to fix "Error: Could not find or load main class" in Eclipse? (guide)
- How to solve java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? (solution)
- 10 common reasons for java.lang.NumberFormatException in Java? (tutorial)
- Cause and solution of "class, interface, or enum expected" compiler error in Java? (fix)
- How to fix the "illegal start of expression" compile-time error in Java? (tutorial)
- How to solve "variable might not have initialized" compile-time error in Java? (answer)
Thanks for reading this troubleshooting guide, if you like this tutorial then please share it with your friends and colleagues too. If you have any doubt or questions then please drop a comment.
No comments :
Post a Comment