Hi guys,
I experience very strange problem. Once a new connection is established and the key for the channel is registered for READ operations, the selector starts to continuosly provide me with the key having OP_READ set up:
if (selector.select() == 0)
{
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext())
{
SelectionKey key = (SelectionKey) it.next();
// Accept new connection
if (key.isAcceptable())
{
ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();
if (channel == null)
{
continue;
}
// Set the new channel nonblocking
channel.configureBlocking(false);
// Register it with the selector
SelectionKey newKey = channel.register(selector, SelectionKey.OP_READ);
// initialization of attachment ...
}
if (key.isReadable())
{
System.out.print("session index = " +((ConnectionDescriptor)key.attachment()).session.sessionIndex);+
+System.out.print(", counter "+ ++counter);+
+System.out.println(", ops="+ key.readyOps());
// read the data from channel
if( counter == 10 ) {
setEnabled(false);
break;
}
}
it.remove();
}
I use a python script in order to test the server:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect("localhost", 28000)
s.send(b'\x00\x01')
s.close()
When I run the script I see the following output from the server:
session index = 1, counter 1, ops=1
session index = 1, counter 2, ops=1
session index = 1, counter 3, ops=1
header limit = 2
body limit = 0
process message 1
session index = 1, counter 4, ops=1
session index = 1, counter 5, ops=1
session index = 1, counter 6, ops=1
session index = 1, counter 7, ops=1
session index = 1, counter 8, ops=1
session index = 1, counter 9, ops=1
session index = 1, counter 10, ops=1
some parts of the code are omitted in order to keep it clear. If I do not stop it at 10th iteration it runs into endless loop.
Why the key always says that the channel is "ready" to be read while I actually have already read the data?
Thanks a lot in advance.
Edited by:
coderman on Jan 28, 2010 5:44 AM