SocketChannel doesn't disconnect

F

Frank Cisco

If I close a SocketChannel like:

((SocketChannel) selectionkey.channel()).close();

How come this returns true when called after the close? Shouldn't it be now
disconnected? And how do you force it to disconnect?

((SocketChannel) selectionkey.channel()).isConnected()
 
T

Thomas Schodt

Frank said:
If I close a SocketChannel like:

((SocketChannel) selectionkey.channel()).close();

How come this returns true when called after the close? Shouldn't it be now
disconnected? And how do you force it to disconnect?

((SocketChannel) selectionkey.channel()).isConnected()


SocketChannel.socket().isConnected() and SocketChannel.isConnected()
return false before the socket is connected.
Once the socket is connected they will return true,
they will not revert to false for any reason.


SocketChannel.isOpen() returns true
until you call SocketChannel.socket().close() or SocketChannel.close().
Once you have called close() on the socket - SocketChannel.isOpen() will
return false,
no other event will cause it to return false.


SocketChannel.socket().isClosed() returns false
until you call SocketChannel.socket().close() or SocketChannel.close().
Once you have called close() on a socket -
SocketChannel.socket().isClosed() will return true,
no other event will cause it to return true.


http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4672570
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6341625
 
F

Frank Cisco

Thomas Schodt said:
SocketChannel.socket().isConnected() and SocketChannel.isConnected()
return false before the socket is connected.
Once the socket is connected they will return true,
they will not revert to false for any reason.


SocketChannel.isOpen() returns true
until you call SocketChannel.socket().close() or SocketChannel.close().
Once you have called close() on the socket - SocketChannel.isOpen() will
return false,
no other event will cause it to return false.


SocketChannel.socket().isClosed() returns false
until you call SocketChannel.socket().close() or SocketChannel.close().
Once you have called close() on a socket -
SocketChannel.socket().isClosed() will return true,
no other event will cause it to return true.


http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4672570
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6341625


What's the point in keeping the SocketChannel open (or it being flagged as
open) if the underlying socket is closed? Surely they're sync'd to open and
close together?
 
T

Thomas Schodt

By calling close().
isConnected() does not do what you expect.
What's the point in keeping the SocketChannel open (or it being flagged as
open) if the underlying socket is closed? Surely they're sync'd to open and
close together?

The SocketChannel and the underlying socket are sync'd, yes.
 
E

EJP

Peter said:
Assuming Java sockets are like most other socket APIs (and I would guess
they are), the "isConnected()" method can only tell you the state of the
socket as of the most recent i/o operation.

Exactly so. Ditto isClosed(), isBound(), etc. They tell you which
methods *you* have called.

It may or may not be worth noting here that channels that are registered
with a Selector don't really close until the next select()/selectNow()
operation. This is obscurely described womewhere in the
java.nio.channels Javadoc which I can never find when I look for it. The
implication for clients that use NIO is that they may need to call
Selector.selectNow() to accelerate the actual closing of a channel.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top