0

Every time I close my TCP Server, on my clients, I get this exception:

    java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:196)
      at java.net.SocketInputStream.read(SocketInputStream.java:122)
      at java.net.SocketInputStream.read(SocketInputStream.java:210)
      at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2293)
      at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2473)
      at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2543)
      at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2615)
      at java.io.ObjectInputStream$BlockDataInputStream.readByte(ObjectInputStream.java:2764)
      at java.io.ObjectInputStream.readByte(ObjectInputStream.java:916)
      at client.ListenFromServer.run(ListenFromServer.java:33)

Here's my two pieces of code, one is my TCPClient which connects to the server through a socket, and also creates a listener to listen to messages from the server:

TCPClient.java: http://pastie.org/private/msbnuhxyevxjlsdq1wvhow

ListenForServer.java: http://pastie.org/private/pav4azrakgfz31culrrg2a

How do I stop this exception from happening? In my while loop in ListenForServer.java, I make a check to see if the socket is closed, and if it is, I break out of the loop, yet I'm still getting this exception.

What's the best way to catch it, because my program does not close properly when I exit it, I have to manually close it from within Netbeans IDE.

Joe Bid
  • 465
  • 8
  • 24
  • This contains different code and has a different outcome, clients remaining active when closed, having to be forced closed in netbeans ide. Still having trouble with this. – Joe Bid May 19 '14 at 05:44
  • The code should be posted here. Otherwise the question has no permanent value and is liable for deletion. – user207421 May 19 '14 at 06:11
  • The reason for this exception is [the same as it always is](http://stackoverflow.com/a/4300803/207421). – user207421 May 19 '14 at 06:21

1 Answers1

2
 In my while loop in ListenForServer.java, I make a check to see if the socket is closed, and if it is, I break out of the loop, yet I'm still getting this exception.

Yes you did but youre still listening to the inputstream when the socket was closed.

you need to catch the socket exception or change ur while loop

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63