Discussion:
Client socket disconnection event not received on Server socket java .nio
(too old to reply)
Avizz
2003-08-14 13:15:08 UTC
Permalink
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
EJP
2003-08-15 00:44:12 UTC
Permalink
Post by 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
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.
Post by Avizz
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.
Post by Avizz
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.
Post by Avizz
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.
Post by Avizz
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.
Leon Lambert
2003-08-15 13:14:23 UTC
Permalink
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
Post by 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
Andy Fish
2003-09-29 10:47:18 UTC
Permalink
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)
Post by 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
Post by 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
Loading...