Java NIO,channels, and interest operations

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I wrote a little network program using Java NIO that can read data
from a channel and write data to the same channel. Currently, it just
reads from the chanel and echoes that same data back. I'm having a
weird problem with selection keys and writing though. As shown in the
code below, I can read when the channel is readable, and I can also
write at the same time. The problem comes if I want to write at any
other time. I've tried checking to see if the channel is writable,
but it never is. I've also read that you shouldn't set a channel to
be writable unless you actually intend to write something because
otherwise the channel will *always* be writable. I've tried writing
after checking for writability, and after the loop below ends, but
neither of those work. WriteMessageToChannel() will only attempt to
write data if there is actually something queued up to write. The
question is how do I get it to work every time, and not just when I
get a readable channel?

My code looks something like this:

while (SelectedKeyIterator.hasNext()) {
//Get the next selection key
SelectionKey NextKey = (SelectionKey)
SelectedKeyIterator.next();

if (NextKey.isValid()) {
//Test whether this key's channel is ready to
accept a new socket connection.

if (NextKey.isAcceptable()) {
SocketChannel AcceptedChannel =
TheConnectionHandler.SetUpConnection(ChannelSelector, NextKey);

//Remove the key we just got from the
iterator's key collection
SelectedKeyIterator.remove();

} else if (NextKey.isReadable()) {
System.out.println("NextKey is Readable");

SocketChannel AcceptedChannel =
(SocketChannel) NextKey.channel();

//Read whatever data is in the channel into a
buffer - works fine
DataHandler.ReadMessageFromChannel
(AcceptedChannel);

//Write whatever data is in the channel into
a buffer - works fine, but I need to be able to do this all the time,
not just when the remote host has sent something for me to read
----> DataHandler.WriteMessageToChannel
(AcceptedChannel); <----
//Remove the key we just got from the
iterator's key collection
SelectedKeyIterator.remove();
}
}
Thanks!
 
J

John B. Matthews

"(e-mail address removed)"
I wrote a little network program using Java NIO that can read data
from a channel and write data to the same channel. Currently, it
just reads from the chanel and echoes that same data back. I'm
having a weird problem with selection keys and writing though. As
shown in the code below, I can read when the channel is readable, and
I can also write at the same time. The problem comes if I want to
write at any other time. I've tried checking to see if the channel
is writable, but it never is. I've also read that you shouldn't set
a channel to be writable unless you actually intend to write
something because otherwise the channel will *always* be writable.
I've tried writing after checking for writability, and after the loop
below ends, but neither of those work. WriteMessageToChannel() will
only attempt to write data if there is actually something queued up
to write. The question is how do I get it to work every time, and
not just when I get a readable channel?

My code looks something like this: [...]
//Write whatever data is in the channel into a buffer - works fine,
but I need to be able to do this all the time, not just when the
remote host has sent something for me to read
----> DataHandler.WriteMessageToChannel(AcceptedChannel); <----
//Remove the key we just got from the iterator's key collection
SelectedKeyIterator.remove();

I have minimal experience with NIO, but this tutorial suggests two
things that might be relevant:

* Set OP_WRITE only when you have data ready
* Alternate between OP_READ and OP_WRITE

<http://rox-xmlrpc.sourceforge.net/niotut/>

Also, please watch tabs & line wrap in code fragments.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top