title: Apache Mina, How to detect when you're sending messages using an invalid socket to the client side?
body: <p>I have a server setup using MINA version 2.
  I don't have much experience with sockets and tcp.</p>
  <p>The problem is if I make a connection to my server, and then unplug my internet and close the connection, (Server doesn't get notification of the connection being closed) the server will forever think that my connection is still active and valid.</p>
  <p>The server will continue to send messages to my connection, and doesn't throw any exceptions even though there is nothing on my computer binded to the local port.</p>
  <p>How can I test that the connection still exists?</p>
  <p>I've tried running MINA logging in debug mode, and logging the</p>
  <pre><code> IoSession.isConnected() IoSession.isActive IoSession.isClosing
  </code></pre>
  <p>They always return true, true, false. Also, in debug mode, there was no useful information stating that the connection was lost. It just logged the regular &quot;sent message&quot; stuff, as if there was nothing wrong.</p>
  <p>From using Flash actionscript, I have had experiences where flash will throw errors that it's operating on an invalid socket. That leads me to believe that it's saying the socket on the server is no longer valid for the connection. So in other words if flash can detect invalid sockets, a Java server should be able to detect it too correct?</p>
  <p>If there is truly no way to detect dead connections, I can always setup a connection keep alive routine where the client is constantly sending an &quot;I'm here&quot; message to the server, and the server closes sessions that havent had an incoming message for a period of seconds.</p>
  <hr />
  <h3>EDIT: After learning that &quot;sockets&quot; are private and never shared over the network I managed to find better results for my issue and I found this SO thread.</h3>
  <p><a href="https://stackoverflow.com/questions/10240694/java-socket-api-how-to-tell-if-a-connection-has-been-closed">Java socket API: How to tell if a connection has been closed?</a></p>
  <p>Unfortunately</p>
  <blockquote>
  <p>IOException 'Connection reset by peer'  Doesn't occur when I write to
  the IoSession in MINA.</p>
  </blockquote>
  <hr />
  <h3>Edit:</h3>
  <p>Is there any way at all in Java to detect when an ACK to a TCP packet was not received after sending a packet? An ACK Timeout?</p>
  <hr />
  <h3>Edit:</h3>
  <p>Yet apparantly, my computer should send a RST to the server? According to this answer. <a href="https://stackoverflow.com/a/1434592/4425643">https://stackoverflow.com/a/1434592/4425643</a>
  But that seems like a bad way of port scanning. Is this how port scanning works? Port scanners send data to a port and the victim's service responds with a RST? Sorry I think I need a new question for all this. But it's odd that MINA doesn't throw connection reset by peer when it sends data. So then my computer <em>doesn't</em> send a RST.</p>