Socket disconnect on client end

B

Bryan

Hello all,

Can anyone tell me how I can tell if a socket has been disconnected on
the client end? I've got a class that creates a SocketServer to listen
on a port and when an connection is initiated it hands the socket off
to a thread. I want to be able to tell when a socket gets disconnected
by the client so I can have a graceful end to the connection's thread.
Right now when a client disconnects I get all kinds of exceptions from
my methods that are reading data from the socket's input stream. I
would like to use InputStream's available() method along with some way
of knowing if the socket still exists before passing the input stream
to my read methods. I tried using Socket's isConnected() method but it
still returns true even after the client disconnects.

Thanks in advance!!!
 
D

Daniel Pitts

Bryan said:
Hello all,

Can anyone tell me how I can tell if a socket has been disconnected on
the client end? I've got a class that creates a SocketServer to listen
on a port and when an connection is initiated it hands the socket off
to a thread. I want to be able to tell when a socket gets disconnected
by the client so I can have a graceful end to the connection's thread.
Right now when a client disconnects I get all kinds of exceptions from
my methods that are reading data from the socket's input stream. I
would like to use InputStream's available() method along with some way
of knowing if the socket still exists before passing the input stream
to my read methods. I tried using Socket's isConnected() method but it
still returns true even after the client disconnects.

Thanks in advance!!!

Catch and handle the exceptions, and then gracefully end the thread.

Although, NIO might be a better approach. I don't know much about it
though, so I can't say for certain.
 
G

garethconner

To test that both ends of the socket are still alive & well, I use the
following inside a class that wraps a standard Socket:

private Socket stagehandSocket;

public boolean isConnected() {
if (stagehandSocket != null)
return (stagehandSocket.isConnected() && stagehandSocket.isBound()
&& !(stagehandSocket.isClosed()) &&
!(stagehandSocket.isInputShutdown()) &&
!(stagehandSocket.isOutputShutdown()));
else
return false;
}
 
B

Bryan

How does one gracefully end a thread? All the obvious methods to use
are depreciated... :-/
 
J

John Ersatznom

Bryan said:
How does one gracefully end a thread? All the obvious methods to use
are depreciated... :-/

Catching the exception in the run() method and then simply returning
from that method isn't deprecated. ;)
 
E

EJP

To test that both ends of the socket are still alive & well, I use the
following inside a class that wraps a standard Socket:

That class can't tell you any such thing. All those APIs tell you is
what methods you have called on your Socket at your end. They tell you
*nothing* about the state of the connection, and *nothing* about what
has happened at the other end.

There are only two ways of telling whether the other end of a TCP
connection is still there:

(a) try to read with a timeout set long enough that tripping it reliably
indicates that the sender has failed, or

(b) try to write to the socket. After you've filled enough send and
receive buffers you will eventually get an IOException or
SocketException "connection reset by peer" if the other end is closed or
aborted.
 
B

Bryan

I appreciate the input guys. I plan on gracefully catching the
EOFException and exiting the thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top