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