• Post Reply Bookmark Topic Watch Topic
  • New Topic

NIO select keeps returning a key

 
N Goldsmith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a client using a non-blocking socket. My server is running and I go and connect to it, then perform the channel.finishConnect(). That all seems to go smoothly and the connection is established. The problem is the select loop keeps returning a key (and I'm stuck in the while(selector...) loop forever). I would think it should only have one key to process, but it goes on and on. Any ideas why it keeps getting back a new key?

//Waiting for the connection
while(selector.select()> 0) {
// Get keys
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();

//For each key...
while (i.hasNext()) {
SelectionKey key = (SelectionKey)i.next();

// Remove the current key
i.remove();

// Get the socket channel held by the key
SocketChannel channel = (SocketChannel)key.channel();

// Attempt a connection
if (key.isConnectable()) {
if (channel.isConnectionPending()){
c = channel.finishConnect();
}
}
}
}
}
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried removing OP_CONNECT from the interest set of the selector ?


Regards,

Pho
 
N Goldsmith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that does work, but I have a similar problem in my main loop. The socket does read/writes on behalf of a client. The problem is I sit at the select, get woken up when a read comes in. I process the read, but next time I do the select, I get woken up with another read that shouldn't be there. This goes on forever.

I'm registered like this:
SelectionKey clientKey = client.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);

then I just added your recommendation:
key.interestOps(key.interestOps() ^ SelectionKey.OP_CONNECT );

Which means I'm just selecting on Reads once I am connected (this works fine and I don't get woken up on connects anymore).

Here is the main loop. I sit at the select the first time I hit it. Once a read comes in, each trip back to the select wakes me up right away with another read (and the socket has no data to read). How do I reset the select so it won't keep sending the same read?

private void Process(){
int keysAdded = 0;
for( ; ; ) {
try{
keysAdded = selector.select();
if(keysAdded > 0 || pending.size() > 0){
keysAdded = 0;
Set readyKeys = this.selector.selectedKeys();
Iterator i = readyKeys.iterator();

while (i.hasNext()) {
SelectionKey key = (SelectionKey)i.next();
i.remove();

if ( key.isReadable() ) {
try{
Read();
}
catch(Exception e){
}
}

}
//Check the queue to see if there are messages to send
if(pending.size() > 0)
processPending();

}
}
catch(Exception e){
System.out.println(e);
}
}
}
[ November 17, 2004: Message edited by: N Goldsmith ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!