Java NIO,channels, and interest operations

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Jul 26, 2009.

  1. Guest

    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!
     
    , Jul 26, 2009
    #1
    1. Advertising

  2. In article
    <>,
    ""
    <> wrote:

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

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jul 26, 2009
    #2
    1. Advertising

  3. Lew Guest

    John B. Matthews wrote:
    > 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.


    Also, also, please watch the coding conventions.
    <http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367>

    --
    Lew
     
    Lew, Jul 26, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,413
    iksrazal
    Jun 18, 2004
  2. Replies:
    0
    Views:
    3,474
  3. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,324
    robert
    Feb 11, 2006
  4. d
    Replies:
    1
    Views:
    478
    Richard Heathfield
    Nov 16, 2003
  5. alex23
    Replies:
    0
    Views:
    425
    alex23
    Apr 2, 2008
Loading...

Share This Page