title: java.net.SocketException: Connection Reset. How do I stop this?
body: <p>Every time I close my TCP Server, on my clients, I get this exception:</p>
  
  <blockquote>
  <pre><code>    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)
  </code></pre>
  </blockquote>
  
  <p>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:</p>
  
  <p>TCPClient.java:
  <a href="http://pastie.org/private/msbnuhxyevxjlsdq1wvhow" rel="nofollow">http://pastie.org/private/msbnuhxyevxjlsdq1wvhow</a></p>
  
  <p>ListenForServer.java:
  <a href="http://pastie.org/private/pav4azrakgfz31culrrg2a" rel="nofollow">http://pastie.org/private/pav4azrakgfz31culrrg2a</a></p>
  
  <p>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.</p>
  
  <p>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.</p>