• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Selector returns set of keys which are already processed in loop before

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi. i am facing problem as following:

i have a Selector and a ServerSocketChanner with non-blocking configuration.

now in main loop, i perform

while(true) {
int keysAdded = selector.select();
if(keysAdded == 0) {
continue;
}
Set keysReady = selector.selectedKeys();

Iterator itrKey = keysReady.iterator();
while(itrKey.hasNext()) {
selectedKey = (SelectionKey) itrKey.next();
itrKey.remove();
}
} //end while

Only single client is connecting to it and closes socket.

when the loop iterates, the selector always returns '1' even at first time, the key is processed and removed.

so how to deselect keys which were selected earlier?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not handling whatever operation is available on the keys, so the next time you do select(), that operation is ready again. Removing the key from the selected set doesn't clear it's ready state, IIRC.

If you get a connection request operation, you must accept or deny it. If you get an I/O ready state, you must read or write appropriately.

Basically, each time through the outermost while loop the Selector is saying, "Some client is waiting to connect." That you tell the Selector that you got the message (removed the key from the selected set) doesn't change the fact that the client is still waiting to connect.
 
Harshil Mehta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi david,

thnx a lot. u r right. it solved my problem...
 
Grow your own food... or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic