Non Blocking SocketChannel and Deregistering Selector keys

R

RFleming

Hello,

I have been working through Java Sockets and Socket channels the last
few days. I have poured over many examples and referenced Java in a
nutshell. I am able to successfully use the socketchannel class in
non blocking mode and to read async as well. For better or for worse
I decided to create a class that allows me to use up to 100
socketchannels using a fixed array. I originally had a continuous
loop monitor the input buffer of all active connections. It worked
really good. I later read about the selector and decided it seemed
more efficient.

As I stated earlier, everything worked really good before the
selector.

With the selector, (I am only using one selector to monitor my up to
100 socketchannels), I am able to successfully register a key for each
socketchannel in the array, open multiple connections, and read data,
non blocking, from those same socketchannels. Once I close any
connection, and later reopen it, the program stops responding. By
stepping thought the code I see it stops when I try to register a
socketchannel's key with the selector that was previously registered.
Meaning socketchannel[0] was opened and registered. Later
socketchannel[0].close was called. Again later socketchannel[0] was
opened, and the program hangs on the register command.

My question is, how can I close a socketchannel and deregister a key
from the selector without closing the selector itself? I don't want
to close the selector because other open sockets would lose their
keys.

below is a small snippet of the code I am using to set up a
socketchannel and one way I tried to prevent multiple registrations of
the selector:

clientsocketchannel[socketindex] = SocketChannel.open();
clientsocketchannel[socketindex].configureBlocking(false);
clientsocketchannel[socketindex].connect(new
InetSocketAddress(ipaddress, socketport));
if (clientsocketchannel[socketindex].keyFor(selector) == null) {
clientsocketchannel[socketindex].register(selector,clientsocketchannel[socketindex].validOps());
}

note on the second time opening a particular socket, both the keyfor
method and isregistered method return null/false, but as soon as the
program tries to register the socketchannel to the selector for a
second time, the debug stepper disappears and my jframe controls stop
responding. I have to press stop in the debugger to quit the program.

I hope I was successful in explaining my problem and as always I
greatly appreciate anyone willing to offer suggestions!

Ryan
 
E

Esmond Pitt

Registering a key with a selector syncrhonizes on the selector. If the
selector is blocked sleeping so will you.

You shouldn't be doing that sort of thing from the AWT thread anyway,
but if you do it in a different thread from the Selector thread you need
to wakeup the selector before the registration.

I would also get rid of the array of socket channels and use the
Selector as the collection of socket channels. Much simpler. You don't
need an external collection of socket channels at all.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top