Friday, April 20, 2012

Java Socket API: How to tell if a socket has been closed?

I am running into some issues with the java Socket API. I am trying to display the number of players currently connected to my game. It is easy to determine when a player has connected. However, it seems unecessarily difficult to determine when a player has disconnected using the socket API.



Calling isConnected() on a socket that has been disconnected remotely always seems to return true. Similarly, calling isClosed() on a socket that has been closed remotely always seems to return false. I have read that to actually determine whether or not a socket has been closed data must be written to the output stream and an exception must be caught. This seems like a really unclean way to handle this situation. We would just constantly have to spam a garbage message over the network to ever know when a socket had closed.



Is there any other solution?





No comments:

Post a Comment