Re: Java NIO Problem
On 6 Dec 2006 20:58:02 -0800, mearvk wrote:
So evidently you have to do something (unintuitive) like this:
Actually, Select only guarantees that you can read once from the
channel without blocking, so the loop isn't just unnecessary here,
it's a bad idea. If or when there is more data available, the key
*will* become active again.
Also, in most applications you probably don't want to read all the way
to EOF as soon as there is data to read.
However you must be prepared to reach EOF at some point, and close()
the channel when that occurs. Otherwise it will continue to be
readable forever, even though there is no data.
I find that this is sufficient:
private void readFromSocketChannel(SelectionKey key)
SocketChannel client = key.channel();
ByteBuffer bb = ByteBuffer.allocate(1024);
if (client.read(bb)) < 0) {
client.close();
}
byte[] bytes = bb.array();
System.out.println(new String(bytes,"ISO-8859-1"));
}
/gordon
--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e