Hello guys, for the past few months, I have been writing about different socket-related errors on Java applications, and today I am going to talk about another common socket-related exception in Java - java.net.SocketException: Connection reset Exception. There is no difference between exceptions thrown by the client and server. This is also very similar to the java.net.SocketException: Failed to read from SocketChannel: Connection reset by a peer but there is some subtle difference. The difference between connection reset and connection reset by peer is that the first means that your side reset the connection, the second means the peer did it. Nothing to do with clients and servers whatsoever
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.
For example, if you are using BufferedReader for writing data then it will block if you don't write \n or line terminator on the message.
Similarly, on the server side you will see the following error :
This occurs when the Server is reading data and blocked on a read() call but the client was terminated, this time you saw the message on the server-side. As you can see we get a SocketException with the message Connection reset. This happens when one of the parties closes the connection without using the close() method.
Btw, this is my fourth article in the series on how to deal with Socket related exceptions in Java. If you haven't read the other articles of this series, here are them
If you struggle to solve socket-related exceptions in client-server Java applications then you can go through those articles to learn more about how to deal with these exceptions.
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
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.
For example, if you are using BufferedReader for writing data then it will block if you don't write \n or line terminator on the message.
Similarly, on the server side you will see the following error :
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source
This occurs when the Server is reading data and blocked on a read() call but the client was terminated, this time you saw the message on the server-side. As you can see we get a SocketException with the message Connection reset. This happens when one of the parties closes the connection without using the close() method.
Btw, this is my fourth article in the series on how to deal with Socket related exceptions 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
- How to deal with java.net.SocketException: Broken pipe exception
If you struggle to solve socket-related exceptions in client-server Java applications then you can go through those articles to learn more about how to deal with these exceptions.
How to solve java.net.SocketException: Connection reset Exception in Java
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
Though, as a Java programmer, you should have good knowledge of Socket API, both old and new and if you need the recommendation to level up your skill, I suggest you join a comprehensive Java course like The Complete Java Programming MasterClass course by Tim Buchalaka on Udemy. It's not only the most comprehensive course but also the most up-to-date and covers news features from recent Java releases.
That's all about how to deal with the java.net.SocketException: Connection reset Exception in Java. As I told you it doesn't matter whether this error is coming on server-side Java application or client-side Java application then underlying cause is always that the other party terminated the connection or the connection is lost due to network issues. You also cannot rule out the possibility of that host is restarted or the server-side application crashed.
Other Java troubleshooting articles you may like:
Thanks for reading this article, if you like this article then please share it with your friends and colleagues too. If you have any doubt or questions then please drop a comment.
P. S. - It's worth noting that both Socket and Network Programming is advanced skill and worth learning. If you want to learn more about Socket Programming in Java, I suggest you check the Java: Socket Programming Simplified, a free course on Udemy.
That's all about how to deal with the java.net.SocketException: Connection reset Exception in Java. As I told you it doesn't matter whether this error is coming on server-side Java application or client-side Java application then underlying cause is always that the other party terminated the connection or the connection is lost due to network issues. You also cannot rule out the possibility of that host is restarted or the server-side application crashed.
Other Java troubleshooting articles you may like:
- java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
- How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error? (hint)
- java.sql.SQLServerException: The index 58 is out of range - JDBC (solution)
- How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger (solution)
- org.Springframework.Web.Context.ContextLoaderListener (solution)
- How to connect to MySQL database in Java? (tutorial)
- Fixing java.lang.UnsupportedClassVersionError Unsupported major.minor version 50.0 (solution)
- How to fix 'javac' is not recognized as an internal or external command (solution)
- java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error (solution)
- Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java? (solution)
- java.lang.ClassNotFoundException :
- How to avoid ConcurrentModificationException in Java? (tutorial)
- How to solve the "could not create the Java virtual machine" error in Java? (solution)
- 10 common reasons for java.lang.NumberFormatException in Java? (tutorial)
- How to fix the "illegal start of expression" compile-time error in Java? (tutorial)
- Cause and solution of "class, interface, or enum expected" compiler error in Java? (fix)
- 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)
- How to solve "variable might not have initialized" compile-time error in Java? (answer)
Thanks for reading this article, if you like this article then please share it with your friends and colleagues too. If you have any doubt or questions then please drop a comment.
P. S. - It's worth noting that both Socket and Network Programming is advanced skill and worth learning. If you want to learn more about Socket Programming in Java, I suggest you check the Java: Socket Programming Simplified, a free course on Udemy.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.