Client socket disconnection event not received on Server socket java .nio

Discussion in 'Java' started by Avizz, Aug 14, 2003.

  1. Avizz

    Avizz Guest

    Hi All
    I question is as below

    i am using java.nio package + win 2K + jdk1.4.1_02

    I have a Server accepting socket connection and is configured
    in Nonblocking mode.

    -The server receives (selector) events (accept event)
    when a client requests for a connection .

    -The server is getting read and write events when
    client wants to read and write

    -The server gets a read event when the client disconnects

    My only problem is that the Server is not getting any events
    when the client disconnect due top network break down
    for EX when the network connection breaks the client disconnects
    but the server selector is not getting any events

    I am storing the client Socket objects in a Hash Table and
    if i execute the .isConnected() method it returns true
    even though the the client is down

    The only work around is to try to read and write on the socket
    i get an IO exception and am able to make out that the
    client is disconnected
    But this is not desirable for my client
    as client modification to give any kind of echo is not possible and
    a read /write event will cause prolbmes.

    Can any one tell how to detect client side disconnection
    with out Read and Write on the client socket

    Avizz, Aug 14, 2003
    1. Advertisements

  2. Avizz

    EJP Guest

    This is because the selector delivers a read event if the socket
    receives a FIN as a result of the client closing its end, but if the
    client just disappears due to the network failing, nothing is received,
    so there *is no* TCP event to deliver. TCP does not know.
    This is not what the isConnected() method is for. It only tells you
    whether you have implicitly or explicitly called Socket.connect() for
    the socket, and it returns true for a socket obtained from
    ServerSocket.accept(). It doesn't tell you anything about the status of
    the other end.
    This is not a 'work around', it is how TCP and sockets are designed.
    What problem will it cause? You will get -1 or an EOFException on a read
    depending on which API you call, or a SocketException after a sufficient
    number of writes.
    The only way to detect a dead connection is to try to use it for I/O.
    EJP, Aug 15, 2003
    1. Advertisements

  3. Avizz

    Leon Lambert Guest

    This is a classic netowrking problem not related to Java. Normal
    solution is to have an alive and well message from both client and
    server. If the message is not recieved in a certain time the connection
    is declared dead.

    Hope this helps.
    Leon Lambert
    Leon Lambert, Aug 15, 2003
  4. Avizz

    Andy Fish Guest

    you can use the TCP built-in KEEPALIVE option for this. Not sure how to set
    it in java though.

    one downside with keepalive is that the timeout can only be specified on a
    per-machine basis (or often cannot be modified at all) and is usually very
    long (several hours)
    Andy Fish, Sep 29, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.