detecting Socket disconnect

Discussion in 'Java' started by bob smith, Oct 17, 2012.

  1. bob smith

    bob smith Guest

    I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

    I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.

    Anyone know how to detect the disconnect?
    bob smith, Oct 17, 2012
    #1
    1. Advertising

  2. bob smith

    Arne Vajhoej Guest

    On 10/17/2012 4:12 PM, bob smith wrote:
    > I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.
    >
    > I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.
    >
    > Anyone know how to detect the disconnect?


    I believe the most reliable method is to implement a protocol that
    contains frequent keep alive messages and use missing such to detect
    a network failure. Otherwise you can try send and wait for timeout.

    Arne
    Arne Vajhoej, Oct 17, 2012
    #2
    1. Advertising

  3. bob smith

    Lew Guest

    bob smith wrote:
    > I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.
    >
    > I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.


    Closed what window?

    How do you know that closing "the window" broke the connection?

    What reads from the other end of the connection?

    > Anyone know how to detect the disconnect?


    http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected()
    "Returns: true if the socket was successfuly connected to a server"

    Your socket was connected, so according to the docs you should get 'true'.

    What does
    http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isClosed()
    tell you?

    --
    Lew
    Lew, Oct 17, 2012
    #3
  4. bob smith

    Arne Vajhoej Guest

    On 10/17/2012 4:47 PM, Lew wrote:
    > bob smith wrote:
    >> I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.
    >>
    >> I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.

    >
    > Closed what window?


    The window of the program he mentioned (Windows telnet) seems
    a good guess.

    The window of his email program would not make much sense.

    > How do you know that closing "the window" broke the connection?


    Broke is a bit fuzzy term.

    But obviously a closed program (Windows telnet terminated when the
    window is closed) can not receive from the connection.

    > What reads from the other end of the connection?


    Whatever uses a java.net.Socket - I would expect a Java program.

    >> Anyone know how to detect the disconnect?

    >
    > http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected()
    > "Returns: true if the socket was successfuly connected to a server"
    >
    > Your socket was connected, so according to the docs you should get 'true'.


    Unfortunately it will only tell status in the apps own end.

    Arne
    Arne Vajhoej, Oct 17, 2012
    #4
  5. bob smith

    Arne Vajhoej Guest

    On 10/17/2012 4:59 PM, Peter Duniho wrote:
    > On Wed, 17 Oct 2012 13:12:13 -0700 (PDT), bob smith wrote:
    >
    >> I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.
    >>
    >> I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.
    >>
    >> Anyone know how to detect the disconnect?

    >
    > Not enough information in your question. What kind of "disconnected" are
    > you asking about?


    He explicit said that the program in the other end was terminated.

    > The normal way to detect a disconnect is to read from the socket and get a
    > return value of 0 bytes for the count of bytes. This tells you that the
    > remote end is done sending (even if it never sent anything) and is ready to
    > close the connection.


    Is that fail fast and reliable?

    > If the connection was reset somehow, such as because of a network failure,
    > then you should get an error (exception) when you attempt to send. There's
    > usually really no point in trying to detect the failure _before_ you would
    > normally send, because the failure could be something in between the end
    > points that recovers. Explicitly attempting to detect the failure can and
    > will create failures that otherwise would not have been a problem.


    That is typical what is done.

    Even though I suspect that it may not always be fast.

    Arne
    Arne Vajhoej, Oct 17, 2012
    #5
  6. bob smith

    Roedy Green Guest

    On Wed, 17 Oct 2012 13:12:13 -0700 (PDT), bob smith
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Anyone know how to detect the disconnect?


    I wrestled with the problem and solved it with a custom protocol
    sending packets back and forth.

    If there is no traffic, it sends a "heartbeat" packet every X seconds,
    just to let the other end know it is alive. The receiver just ignores
    them. If a read times out, it presumes the worst and restarts the
    socket.

    TCP/IP is quite happy with no traffic for long periods of time, so it
    is up to you if you want early notification.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    There are four possible ways to poke a card into a slot.
    Nearly always, only one way works. To me that betrays a
    Fascist mentality, demanding customers conform to some
    arbitrary rule, and hassling them to discover the magic
    orientation. The polite way to do it is to design the reader
    slot so that all four ways work, or so that all the customer
    has to do is put the card in the vicinity of the reader.
    Roedy Green, Oct 17, 2012
    #6
  7. On 17/10/12 21:59, Peter Duniho wrote:
    > On Wed, 17 Oct 2012 13:12:13 -0700 (PDT), bob smith wrote:
    >> I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

    > The normal way to detect a disconnect is to read from the socket and get a
    > return value of 0 bytes for the count of bytes.


    This is the case in C, where functions like recv() use -1 to signal an
    error and 0 for EOF. However, in Java, InputStream (such as you get
    from a Socket) returns -1 to signal EOF.

    <http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html>


    --
    ss at comp dot lancs dot ac dot uk
    Steven Simpson, Oct 18, 2012
    #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. Guest

    Socket & Disconnect

    Guest, Jan 10, 2006, in forum: Java
    Replies:
    10
    Views:
    18,052
    Chris Uppal
    Jan 14, 2006
  2. Bryan
    Replies:
    6
    Views:
    877
    Bryan
    Dec 20, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,810
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    961
    Jean-Paul Calderone
    Jan 27, 2009
  5. Jon Fi
    Replies:
    1
    Views:
    150
    hemant
    Oct 3, 2006
Loading...

Share This Page