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

    Regards
    Avinash
     
    Avizz, Aug 14, 2003
    #1
    1. Advertising

  2. Avizz

    EJP Guest

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

    Avizz wrote:
    >
    > 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


    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.

    > 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


    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.

    > The only work around is to try to read and write on the socket


    This is not a 'work around', it is how TCP and sockets are designed.

    > 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.


    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.

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


    The only way to detect a dead connection is to try to use it for I/O.
     
    EJP, Aug 15, 2003
    #2
    1. Advertising

  3. Avizz

    Leon Lambert Guest

    Re: Client socket disconnection event not received on Server socketjava .nio

    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

    Avizz wrote:
    > 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
    >
    > Regards
    > Avinash
     
    Leon Lambert, Aug 15, 2003
    #3
  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)


    "Leon Lambert" <> wrote in message
    news:...
    > 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
    >
    > Avizz wrote:
    > > 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
    > >
    > > Regards
    > > Avinash

    >
     
    Andy Fish, Sep 29, 2003
    #4
    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. Jason Teagle
    Replies:
    12
    Views:
    3,412
    Jason Teagle
    Oct 5, 2003
  2. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,411
    iksrazal
    Jun 18, 2004
  3. DennyOR
    Replies:
    10
    Views:
    1,178
    Chris Uppal
    May 13, 2005
  4. brieuc
    Replies:
    5
    Views:
    2,282
    Esmond Pitt
    Mar 20, 2007
  5. Sunny Mohan
    Replies:
    0
    Views:
    227
    Sunny Mohan
    Apr 5, 2004
Loading...

Share This Page