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)
---------------------------------------------------------------------------------

2 comments:

  1. I am getting the below error after upgrade:

    java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168)

    This is an intermittent issue. Can you please help me resolve this.

    ReplyDelete
  2. Slots - Drive & Play – DRMCD
    Drive & Play. Play games you love from your desktop 논산 출장마사지 or your mobile device. 인천광역 출장안마 Whether 순천 출장안마 you are a 논산 출장안마 gamer, 사천 출장마사지 a beginner, a professional, or an advanced gamer who

    ReplyDelete