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