1

Do you know how can I keep alive a socket connection if I do not do any actions on the socket? I just noticed that if my connection is on background and I do not operate it I get this:

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.writeInt(Unknown Source)

So, how can I control that the connection will not be lost after some idle time and after how much time by default does it happen?

Mulder
  • 25
  • 5

2 Answers2

2

This problem has no general solution. It is quite likely that the remote server / service that has decided to reset/close the TCP/IP connection. How and why it decides is application specific. An application specific solution will be required to prevent it.

FWIW, the SO_KEEP_ALIVE mechanism causes the protocol stack to occasionally exchange messages on an otherwise idle TCP/IP connection. This may help if you are experiencing resets on a NATed connection due to port reuse. The relevant Java method is Socket.setKeepAlive.

user207421
  • 305,947
  • 44
  • 307
  • 483
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Connection reset by peer: socket write error

This is usually caused by writing to a connection that has already been closed by the peer. It is an application protocol error. You should investigate that first before worrying about connection lifetimes.

user207421
  • 305,947
  • 44
  • 307
  • 483