I want to be able to set a boolean (bConnected) to true if the socket is disconnected. I asume this would be done easiest in my loop to receive data, but any other suggested places are also welcomed.
I don't know of any way to detect if the remote end of the socket has created a disconnect. I want to distroy finished threads (defined by the socket being closed), but its kinda hard to do if I don't know when its finished.
Does anyone know how to go about doing this?
Depends on how you implement your sockets.
I've got a socket listening, and constantly accepting connections, which are forked to a new handling class, with the socket passed in its constructor. What I want to do is loop through the classes (that's easy), and check if its still connected, and if not, set it = null.
public SocketAddress getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
Socket (http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#getRemoteSocketAddress())
Pssh. You rock. =)
EDIT -
Eh, it still doesn't work. =(
EDIT -
Quotea SocketAddress reprensenting the remote endpoint of this socket, or null if it is not connected yet.
Yeah, it only returns null if it hasn't been connected yet. Same as isConnected().
I've taken iago's suggestion of sending a keepalive packet, and if it errors on sending you've been disconnected.
YOU LIAR I SUGGESTED THAT LIKE 4 DAYS AGO OMG.
iago suggested it five. G_G.