NIO writing to a channel

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

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

Roedy Green

On Mon, 3 Aug 2009 00:08:09 -0700 (PDT),
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)
 
N

nooneinparticular314159

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

Roedy Green

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

EJP

Roedy said:
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.
 
E

EJP

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.
 

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

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top