Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Socket disconnect on client end

Thread view: 
Bryan - 19 Dec 2006 00:08 GMT
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!!!
Daniel Pitts - 19 Dec 2006 00:21 GMT
> Hello all,
>
[quoted text clipped - 11 lines]
>
> 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.
Bryan - 19 Dec 2006 04:06 GMT
How does one gracefully end a thread?  All the obvious methods to use
are depreciated... :-/

On Dec 18, 5:21 pm, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> > Hello all,
>
[quoted text clipped - 14 lines]
> Although, NIO might be a better approach. I don't know much about it
> though, so I can't say for certain.- Hide quoted text -- Show quoted text -
John Ersatznom - 19 Dec 2006 05:02 GMT
> 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. ;)
garethconner@mac.com - 19 Dec 2006 02:20 GMT
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;
    }
EJP - 19 Dec 2006 06:28 GMT
> 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.
Bryan - 20 Dec 2006 16:13 GMT
I appreciate the input guys.  I plan on gracefully catching the
EOFException and exiting the thread.

> garethcon...@mac.com wrote:
> > To test that both ends of the socket are still alive & well, I use the
[quoted text clipped - 13 lines]
> SocketException "connection reset by peer" if the other end is closed or
> aborted.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.