Socket disconnect on client end

Discussion in 'Java' started by Bryan, Dec 19, 2006.

  1. Bryan

    Bryan Guest

    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!!!
     
    Bryan, Dec 19, 2006
    #1
    1. Advertising

  2. Bryan

    Daniel Pitts Guest

    Bryan wrote:
    > 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.
     
    Daniel Pitts, Dec 19, 2006
    #2
    1. Advertising

  3. Bryan

    Guest

    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;
    }
     
    , Dec 19, 2006
    #3
  4. Bryan

    Bryan Guest

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

    On Dec 18, 5:21 pm, "Daniel Pitts" <>
    wrote:
    > Bryan wrote:
    > > 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.- Hide quoted text -- Show quoted text -
     
    Bryan, Dec 19, 2006
    #4
  5. Bryan wrote:
    > 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. ;)
     
    John Ersatznom, Dec 19, 2006
    #5
  6. Bryan

    EJP Guest

    wrote:
    > 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.
     
    EJP, Dec 19, 2006
    #6
  7. Bryan

    Bryan Guest

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

    On Dec 18, 11:36 pm, EJP <> wrote:
    > wrote:
    > > 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, Dec 20, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. farseer
    Replies:
    7
    Views:
    919
    Esmond Pitt
    Apr 23, 2005
  2. Guest

    Socket & Disconnect

    Guest, Jan 10, 2006, in forum: Java
    Replies:
    10
    Views:
    18,141
    Chris Uppal
    Jan 14, 2006
  3. =?Utf-8?B?QXJuZSBHYXJ2YW5kZXI=?=

    Client Disconnect

    =?Utf-8?B?QXJuZSBHYXJ2YW5kZXI=?=, Nov 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    401
    =?Utf-8?B?QXJuZSBHYXJ2YW5kZXI=?=
    Nov 3, 2006
  4. Jon Fi
    Replies:
    1
    Views:
    168
    hemant
    Oct 3, 2006
  5. bob smith

    detecting Socket disconnect

    bob smith, Oct 17, 2012, in forum: Java
    Replies:
    6
    Views:
    622
    Steven Simpson
    Oct 18, 2012
Loading...

Share This Page