NIO writing to a channel

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Aug 3, 2009.

  1. Guest

    I'm trying to write to a channel using NIO, and I've created a little
    echo server to test this. My client does indeed echo properly. But
    I'd like to try sending some information while the remote client is
    sitting there waiting. The problem is, no matter what I do to set my
    channel as writable, data is still only transmitted when the channel
    has previously become readable from the remote client transmitting
    data. For example, I can try the following on the channel:

    public void RegisterChannelForWriting(SocketChannel Channel,
    Selector S) {

    //Register the socket channel for reading
    try {
    Channel.register(S, SelectionKey.OP_WRITE);
    } catch (IOException Exception) {
    }
    }

    But despite supposedly being registered for writing, data is never
    written to the channel unless the remote client sends data first. Any
    idea what is wrong? How can I fix this?

    Thanks!
    , Aug 3, 2009
    #1
    1. Advertising

  2. Roedy Green Guest

    On Mon, 3 Aug 2009 00:08:09 -0700 (PDT),
    ""
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >But despite supposedly being registered for writing, data is never
    >written to the channel unless the remote client sends data first. Any
    >idea what is wrong? How can I fix this?


    You have to have an open socket first. This is usually done by the
    client sending some data and the server doing a:

    ServerSocket echo = new ServerSocket( PORT );
    Socket socket = echo.accept();

    If you want the server to send first, then the client is going to have
    to do the ServerSocket.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
    ~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)
    Roedy Green, Aug 3, 2009
    #2
    1. Advertising

  3. Err..let me rephrase. The server sits there and when the client
    connects, a serverSocket gets opened. I'm establishing the connection
    fine. What I mean is that I have a queue of data that I generate on
    the server. The server checks to see if data is in the queue. If
    there is, it attempts to write it to the channel. If the server
    attempts a write, but writes 0 bytes, then it registers the channel
    for writing and tries again, then reregisters the channel for
    reading. The channel is normally registered for reading.

    The problem is that the server only manages to write when data comes
    in from the client (ie. when the channel is ready for reading). In
    other words, let's say that I connect to the server with a telnet
    session. The server will sit there and generate data to send to the
    client, but will only actually transmit it when I type something into
    the client and the client sends that data. At that point, the server
    sends everything that has accumulated in its queue.

    What should happen is if the server has some data to write, it gets
    written, whether or not the client has just sent something. It seems
    like I must be doing something wrong with the interest ops, since this
    isn't happening. Any ideas?

    Thanks!
    nooneinparticular314159, Aug 3, 2009
    #3
  4. Roedy Green Guest

    On Mon, 3 Aug 2009 10:45:18 -0700 (PDT), nooneinparticular314159
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Err..let me rephrase. The server sits there and when the client
    >connects, a serverSocket gets opened. I'm establishing the connection
    >fine. What I mean is that I have a queue of data that I generate on
    >the server. The server checks to see if data is in the queue. If
    >there is, it attempts to write it to the channel. If the server
    >attempts a write, but writes 0 bytes, then it registers the channel
    >for writing and tries again, then reregisters the channel for
    >reading. The channel is normally registered for reading.


    I have never used nio for socket i/o, so I am just guessing here.

    As I understand it, nio is layered on top of ordinary socket i/o
    streams. With regular sockets you use one stream to read and one to
    write, with two threads. I don't yet understand what nio buys you with
    that extra layer of overhead.

    It would seem to me then, you should have two NIO channels.

    If you used only one channel, and kept flipping its mode, you would
    likely get some sort of backlog clear on each flip which would discard
    data.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
    ~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)
    Roedy Green, Aug 4, 2009
    #4
  5. EJP Guest

    Roedy Green wrote:
    > As I understand it, nio is layered on top of ordinary socket i/o
    > streams.


    No. NIO is not layered on top of streams. It is a completely separate
    I/O abstraction.

    > I don't yet understand what nio buys you with
    > that extra layer of overhead.


    That's because there isn't an extra layer of overhead. What NIO buys you
    is the ability to manage all the socket channels via a single thread.
    EJP, Aug 4, 2009
    #5
  6. EJP Guest

    wrote:
    > try {
    > Channel.register(S, SelectionKey.OP_WRITE);
    > } catch (IOException Exception) {
    > }


    You appear to be suppressing an exception there. That might be a problem.

    But as you have read, OP_WRITE is almost always ready, except when the
    corresponding socket send buffer is full, which certainly won't be the
    case with a brand new connection.

    Post some code.
    EJP, Aug 4, 2009
    #6
  7. EJP Guest

    Steven Simpson wrote:
    > Doesn't that cancel interest in OP_READ?


    Exactly so. If you want to preserve the existing interestOps you have to
    OR them together.
    EJP, Aug 5, 2009
    #7
    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,217
    iksrazal
    Jun 18, 2004
  2. farseer
    Replies:
    2
    Views:
    460
    farseer
    Apr 11, 2005
  3. Replies:
    6
    Views:
    660
    Gordon Beaton
    Mar 17, 2008
  4. Replies:
    15
    Views:
    920
  5. Jan Burse
    Replies:
    10
    Views:
    391
    Arne Vajhøj
    Nov 6, 2011
Loading...

Share This Page