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

A

Avizz

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
 
E

EJP

Avizz said:
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.
 
L

Leon Lambert

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
 
A

Andy Fish

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)
 

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

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top