Detection other side of socket closed

Discussion in 'Java' started by Vincent Cantin, Sep 9, 2004.

  1. Hi,

    Here is my problem :

    I am developing a client/server application. I have my server which only
    have 1 thread to process the requests of all the clients. I need that none
    of the read it makes from the input streams of the socket to be blocking, so
    I am reading the bytes available in the input stream before I do my reads so
    I am never in a blocking state.

    Now, my problem is that I don't know how to detect when a client close his
    socket. I saw on the net that usually the programmers are reading the input
    stream or writing the output stream in order to receive an IOException, but
    when the client close his socket, the available number of byte in the input
    stream of the server doesn't change, and I don't know how to make the
    detection.

    Do you see the paradox ? I need to call a blocking function if I want to to
    detect the close of the socket, but in my server I cannot afford to call a
    blocking function.

    If someone have a magical way to detect in a non-blocking way when a client
    close normally his socket, then I will really be happy to heard it.

    Thank you,
    Vincent
    Vincent Cantin, Sep 9, 2004
    #1
    1. Advertising

  2. Vincent Cantin

    Rune Fauske Guest

    In article <>,
    says...
    > Hi,
    >
    > Here is my problem :
    >
    > I am developing a client/server application. I have my server which only
    > have 1 thread to process the requests of all the clients. I need that none
    > of the read it makes from the input streams of the socket to be blocking, so
    > I am reading the bytes available in the input stream before I do my reads so
    > I am never in a blocking state.
    >
    > Now, my problem is that I don't know how to detect when a client close his
    > socket. I saw on the net that usually the programmers are reading the input
    > stream or writing the output stream in order to receive an IOException, but
    > when the client close his socket, the available number of byte in the input
    > stream of the server doesn't change, and I don't know how to make the
    > detection.


    You should probably look into Java NIO
    [http://java.sun.com/j2se/1.4.2/docs/guide/nio/]

    >
    > Do you see the paradox ? I need to call a blocking function if I want to to
    > detect the close of the socket, but in my server I cannot afford to call a
    > blocking function.
    >
    > If someone have a magical way to detect in a non-blocking way when a client
    > close normally his socket, then I will really be happy to heard it.


    Here is an example on how to detect a remote socket close when using
    Java NIO [http://javaalmanac.com/egs/java.nio/DetectClosed.html]

    --
    Rune Fauske
    Rune Fauske, Sep 9, 2004
    #2
    1. Advertising

  3. Rune Fauske wrote:

    > In article <>,
    > says...
    >
    >> ... how to detect when a client close his
    >>socket.

    >
    > You should probably look into Java NIO
    > [http://java.sun.com/j2se/1.4.2/docs/guide/nio/]
    >
    >>If someone have a magical way to detect in a non-blocking way when a client
    >>close normally his socket, then I will really be happy to heard it.

    >
    > Here is an example on how to detect a remote socket close when using
    > Java NIO [http://javaalmanac.com/egs/java.nio/DetectClosed.html]


    It is my understanding that the only guaranteed way of detecting a
    broken socket is to use write(); implement a heartbeat/handshake message.

    In the case of a (persisting) break on a network cable between two
    transit points (routers/switches/hubs) how would the local TCP/IP stack
    know that a pending or new read() should throw an exception?
    Thomas Schodt, Sep 9, 2004
    #3
  4. On Thu, 09 Sep 2004 10:44:13 +0100, Thomas Schodt wrote:
    > It is my understanding that the only guaranteed way of detecting a
    > broken socket is to use write(); implement a heartbeat/handshake
    > message.
    >
    > In the case of a (persisting) break on a network cable between two
    > transit points (routers/switches/hubs) how would the local TCP/IP
    > stack know that a pending or new read() should throw an exception?


    When a socket is closed by the remote (as indicated in the original
    post) either read() or write() on the corresponding socket streams can
    and should be used to detect EOF. As another poster suggested, the
    Java NIO classes can be used instead if non-blocking behaviour is
    needed.

    A broken physical link is not the same as a closed socket. Since TCP
    attempts to handle the situation and recover, communication could
    potentially still resume on the same connection. However if you want
    to detect that condition before TCP gives up (as it eventually will),
    then yes, an application level heartbeat is necessary. It's important
    to realize though that you lose some of the resiliency that TCP offers
    if you are too quick to close a non-responsive connection.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Sep 9, 2004
    #4
  5. > > Hi,
    > >
    > > Here is my problem :
    > >
    > > I am developing a client/server application. I have my server which only
    > > have 1 thread to process the requests of all the clients. I need that

    none
    > > of the read it makes from the input streams of the socket to be

    blocking, so
    > > I am reading the bytes available in the input stream before I do my

    reads so
    > > I am never in a blocking state.
    > >
    > > Now, my problem is that I don't know how to detect when a client close

    his
    > > socket. I saw on the net that usually the programmers are reading the

    input
    > > stream or writing the output stream in order to receive an IOException,

    but
    > > when the client close his socket, the available number of byte in the

    input
    > > stream of the server doesn't change, and I don't know how to make the
    > > detection.

    >
    > You should probably look into Java NIO
    > [http://java.sun.com/j2se/1.4.2/docs/guide/nio/]
    >
    > >
    > > Do you see the paradox ? I need to call a blocking function if I want to

    to
    > > detect the close of the socket, but in my server I cannot afford to call

    a
    > > blocking function.
    > >
    > > If someone have a magical way to detect in a non-blocking way when a

    client
    > > close normally his socket, then I will really be happy to heard it.

    >
    > Here is an example on how to detect a remote socket close when using
    > Java NIO [http://javaalmanac.com/egs/java.nio/DetectClosed.html]
    >
    > --
    > Rune Fauske


    Thank for all of you for the usefull information.
    My server will be a success with them.
    Java rulez :)
    Vincent Cantin, Sep 10, 2004
    #5
    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. Dave Rudolf
    Replies:
    3
    Views:
    11,186
    Esmond Pitt
    Dec 31, 2003
  2. billiejoex
    Replies:
    6
    Views:
    1,965
    Roy Smith
    Jul 27, 2007
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,755
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    942
    Jean-Paul Calderone
    Jan 27, 2009
  5. Matt Kruse
    Replies:
    5
    Views:
    293
    Richard Cornford
    Sep 9, 2003
Loading...

Share This Page