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.