Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Selector returns SelectionKey with OP_READ without data in infinity loop after writing to channel
    primarykey
    data
    text
    <p>I've trouble with my code: i've written simple SocketChannel client with Selector, after starting it successfully reads messages from server (server sends events). But after writing to socket (see main method) selector starts returning readable socket in infinyty loop, handleKey returns that -1 bytes readed, so selector all time returns OP_READ SelectionKey without data for reading. Sorry for my English. </p> <p>Thanks.</p> <pre class="lang-java prettyprint-override"><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; public class SelectorTest { public SelectorTest() throws IOException { selector = Selector.open(); } private void runSelector() { new Thread(new Runnable() { public void run() { alive = true; try { while(alive) { System.out.println("Selector started..."); selector.select(); Iterator&lt;SelectionKey&gt; keyIter = selector.selectedKeys().iterator(); while(keyIter.hasNext()) { SelectionKey key = keyIter.next(); keyIter.remove(); handleKey(key); } } } catch (IOException ex) { ex.printStackTrace(); } } }).start(); } private void handleKey(SelectionKey key) throws IOException { SocketChannel chan = (SocketChannel) key.channel(); System.out.println("Processing selected..."); if(key.isConnectable()) { System.out.println("Connecting ..."); if(chan.finishConnect()) { key.interestOps(SelectionKey.OP_READ); } else { key.channel(); } } else if(key.isReadable()) { System.out.println("Processing reading..."); ByteBuffer buf = ByteBuffer.allocate(1024); int readedBytes = chan.read(buf); System.out.println("Readed: " + readedBytes); buf.flip(); for(byte b : buf.array()) { System.out.print((char) b); } } else if(key.isWritable()) { System.out.println("Finishing writing..."); key.interestOps(SelectionKey.OP_READ); } } public static void main(String[] args) throws IOException { SocketChannel channel = SocketChannel.open(); channel.configureBlocking(false); channel.connect(new InetSocketAddress("t1.sis.lan", 6001)); SelectorTest ds = new SelectorTest(); ds.runSelector(); channel.register(ds.selector, SelectionKey.OP_CONNECT); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for(;;) { String line = in.readLine(); if(line==null) break; if(line.toLowerCase().equals("bye")) break; if (line.toLowerCase().equals("write")) { String command = "GET_STREAMS\r\n\0"; ByteBuffer buf = ByteBuffer.allocate(1024); buf.put(command.getBytes()); buf.flip(); channel.write(buf); } System.out.println("echo: "+line); // is it alive check } ds.alive = false; ds.selector.wakeup(); channel.close(); } private Selector selector; private boolean alive; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload