简体   繁体   中英

Underlying Workings of SelectionKey.interestOps(int ops)

I understand that Server socket channel is registered to listen for accept, when accepted a channel is registered for read and once read it is registered for write and this is done by adding the relevant keys to the SelectionKey's interest set using the interestOps method.

However, when we remove some interestOps from a key for eg key.interestOps(key.interestOps() & ~SelectionKey.OP_READ);

What actually happens here? Does this mean that the server will just not listen for any incoming requests to channel belonging to this socket, and the source channel will be oblivious of this decision by the server and might keep on sending data to the server? Or will it somehow inform the channel source of this decision.

In packet switching parlance, is the above operation effectively the same as server receiving packets and just dropping the packet if the interestKeys for the channel this packet belong to have been "unset"

However, when we remove some interestOps from a key for eg key.interestOps(key.interestOps() & ~SelectionKey.OP_READ);

What actually happens here?

What literally happens is something like:

public void interestOps(int interestOps)
{
    this.interestOps = interestOps;
}

Does this mean that the server will just not listen for any incoming requests to channel belonging to this socket

It means that the Selector won't trigger any OP_READ events if data arrives via the socket. It doesn't mean that data won't be received.

and the source channel will be oblivious of this decision by the server and might keep on sending data to the server?

If by 'source channel' you mean the peer, it is not advised in anyway, unless the receive buffer fills up at the receiver.

Or will it somehow inform the channel source of this decision.

No.

In packet switching parlance, is the above operation effectively the same as server receiving packets and just dropping the packet if the interestKeys for the channel this packet belong to have been "unset".

No.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM