Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: Joe[x86] on December 04, 2005, 08:44 PM

Title: Detecting remote socket disconnects?
Post by: Joe[x86] on December 04, 2005, 08:44 PM
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?
Title: Re: Detecting remote socket disconnects?
Post by: The-FooL on December 04, 2005, 10:22 PM
Depends on how you implement your sockets.
Title: Re: Detecting remote socket disconnects?
Post by: Joe[x86] on December 05, 2005, 07:05 AM
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.
Title: Re: Detecting remote socket disconnects?
Post by: The-FooL on December 05, 2005, 02:26 PM

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())
Title: Re: Detecting remote socket disconnects?
Post by: Joe[x86] on December 05, 2005, 05:34 PM
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().
Title: Re: Detecting remote socket disconnects?
Post by: Joe[x86] on December 05, 2005, 05:45 PM
I've taken iago's suggestion of sending a keepalive packet, and if it errors on sending you've been disconnected.
Title: Re: Detecting remote socket disconnects?
Post by: Warrior on December 05, 2005, 08:20 PM
YOU LIAR I SUGGESTED THAT LIKE 4 DAYS AGO OMG.
Title: Re: Detecting remote socket disconnects?
Post by: Joe[x86] on December 06, 2005, 07:04 AM
iago suggested it five. G_G.