-1

I'm trying to find out, why the selector does not work. Here is the code:

//SocketChannel ch
//java.nio.channels.Selector impl
//Object session
SelectionKey sk = ch.register(impl, 0x01, session);
int i = impl.select();
System.out.println(i);

The value printed is zero. This is the only call of SocketChannel.register in the program, so, as far as I understand:
a) one new SelectionKey should be created during register;
b) therefore, "ready-operation set" of one key should be updated during the select, i.e. this method should return 1.

Why does it not work as i expect? What exactly means "whose ready-operation sets were updated"?

4
  • Why? What's 'ch'? What state is it in? What makes you think (b) follows from (a)?
    – user207421
    May 11, 2013 at 11:42
  • Okay, then when are "ready-operation sets updated"
    – i.y.
    May 11, 2013 at 11:47
  • System.out.println(ch.isBlocking()); System.out.println(ch.isConnected()); System.out.println(ch.isConnectionPending()); System.out.println(ch.isOpen()); System.out.println(ch.isRegistered()); System.out.println(ch.getRemoteAddress()); gives false true false true true /192.168.1.249:14712
    – i.y.
    May 11, 2013 at 11:52
  • When an operation becomes ready. What operation do you think is ready?
    – user207421
    May 11, 2013 at 17:48

1 Answer 1

1

You don't seem to understand the meaning of the terms you are using. 'Ready' means 'ready to execute an I/O operation without blocking'. For example, OP_READ being ready means that a read() will return either a positive integer indicating a number of bytes that have been read, or -1 indicating end of stream. Specifically, it will not return zero, indicating that there is no data available to be read.

There is nothing in your code that proves that the select() should return with any selected keys. Only some external event from the peer can cause that, and you've said nothing about that whatsoever.

NB Don't use the magic number 0x01. There are manifest constants for the selection events: SelectionKey.OP_READ and friends. Use them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.