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.

3
  • 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. Commented May 19, 2014 at 5:44
  • The code should be posted here. Otherwise the question has no permanent value and is liable for deletion. Commented May 19, 2014 at 6:11
  • The reason for this exception is the same as it always is. Commented May 19, 2014 at 6:21

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.