android - With a non-blocking SocketChannel, is the affiliated Socket blocking? -
i'm developing android app, trying non-blocking write 1 thread on socket, while doing blocking read on thread. i'm looking through socketchannel docs , trying figure out configureblocking does. specifically, if have non-blocking socketchannel, , access affiliated socket socketchannel.socket(), socket non-blocking in way? or blocking?
in other words, can effect of 1 blocking direction , 1 non-blocking direction having non-blocking socketchannel non-blocking direction, , using affiliated socket other direction?
if socket
has associated socketchannel
, cannot directly read it's inputstream
. you'll illegalblockingmodeexception
. see here.
you can block on non-blocking socketchannels registering them selector , using select() or select(long timeout). methods block until registered channel ready (or timeout expired).
the channel still non-blocking threads not using selector.
modified example here:
selector selector = selector.open(); channel.configureblocking(false); // register op_read: interested in reading channel channel.register(selector, selectionkey.op_read); while (true) { int readychannels = selector.select(); // 1 blocks... // safety net if selector awoke other means if (readychannels == 0) continue; set<selectionkey> selectedkeys = selector.selectedkeys(); iterator<selectionkey> keyiterator = selectedkeys.iterator(); while (keyiterator.hasnext()) { selectionkey key = keyiterator.next(); keyiterator.remove(); if (!key.isvalid()) { continue; } else if (key.isacceptable()) { // connection accepted serversocketchannel. } else if (key.isconnectable()) { // connection established remote server. } else if (key.isreadable()) { // channel ready reading } else if (key.iswritable()) { // channel ready writing } } }
Comments
Post a Comment