Showing posts with label Exceptions. Show all posts
Showing posts with label Exceptions. Show all posts

Monday, January 25, 2010

SocketException in Java

In this post we will look into the details of 2 kinds of SocketException in Java
1) java.net.SocketException: Connection reset and
2) java.net.SocketException: Connection reset by peer: socket write error

1) java.net.SocketException: Connection reset

Occurs when :
While reading from the socket(inputstream)

Occurs where:
Server side or Client side

Reason:
If program(server/client) tries to read data from (client/server) using sockets InputStream and the other side(client/server) has gone down unexpectedly i.e crashed without closing its sockets or OutputStream.

However:
Note if the other(client/server) side closes its socket or related OutputStream (belonging to socket),this exception does not occur at the program(server/client) side.Closing the socket closes all the related output and input streams.

Summary:
If program tries to read from a socket input stream and the other side has gone down(crashed),or thread completes its run without closing the outputstream or socket.

Stack Trace:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.net.SocketInputStream.read(SocketInputStream.java:182)
at java.io.FilterInputStream.read(FilterInputStream.java:66)
at SocketServerDemo.main(SocketServerDemo.java:27)
---------------------------------------------------------------------------------
2) java.net.SocketException: Connection reset by peer: socket write error

Occurs when :
While writing to the socket(outputstream)

Occurs where:
Server side or Client side

Reason:
If program(server/client) tries to write data to (client/server) using sockets OutputStream and the other side(client/server) has gone down unexpectedly i.e crashed without closing its sockets or InutputStream.

However:
Note if the other(client/server) side closes its socket or related InputStream (belonging to socket), this exception does not occur at the program(server/client) side.Closing the socket in turn closes all the related output and input streams.

Summary:
If program tries to write to the socket's output stream and the other side has gone down(crashed),or thread completes its run without closing the inputstream or socket.

Stack Trace:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.write(DataOutputStream.java:71)
at ConnectionResetServerDemo_1.main(ConnectionResetServerDemo_1.java:15)
---------------------------------------------------------------------------------

How to convert Exception Stack Trace to String in Java ?

Below code snippet shows how to convert a Exception Stack Trace to String object in Java

public static String convertExStackTraceToString(Exception ex)

{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
sw.flush();
String strStackTrace = sw.toString();
try
{
sw.close();
pw.close();
}catch (IOException ex1)
{
ex1.printStackTrace();
}
return strStackTrace;
}