How to close a socket correctly after an exception?

After my last project, I had a problem that the client was expecting an object from the server, but an exception was thrown when processing clients, forcing the server to close the socket for security reasons.

This leads to the fact that the client ends in a very unpleasant way, since I decided to deal with this, it is to send the client a message about the input status after each input received, so that he knows whether his input is processed correctly or if he needs an exception.

So my question is:

  • Is there a better / cleaner way to close a socket after throwing an exception?
+5
source share
4 answers

, , , .

Socket , , setSoTimeout. , 5 , , , java.net.SocketTimeoutException, .

ScheduledExecutorService Timer .

+2

:

  • try/catch.
  • "finally". , , , .
+2

. , .

, OP, EOFException IOException: 'connection reset' SocketTimeoutException.

+1
source

You really can't, since the socket is closed, you can listen to it for

java.net.SocketException: socket closed 

and then you will find out that you lost your connection to the server.

0
source

All Articles