Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Short answer: remove <code>OP_CONNECT</code> from the list of operations you are interested in for the accepted connection -- an accepted connection is already connected.</p> <p>I managed to reproduce the issue, which might be exactly what's happening to you:</p> <pre><code>import java.net.*; import java.nio.channels.*; public class MyNioServer { public static void main(String[] params) throws Exception { final ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(true); serverChannel.socket().bind(new InetSocketAddress("localhost", 12345)); System.out.println("Listening for incoming connections"); final SocketChannel clientChannel = serverChannel.accept(); System.out.println("Accepted connection: " + clientChannel); final Selector selector = Selector.open(); clientChannel.configureBlocking(false); final SelectionKey clientKey = clientChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_CONNECT); System.out.println("Selecting..."); System.out.println(selector.select()); System.out.println(selector.selectedKeys().size()); System.out.println(clientKey.readyOps()); } } </code></pre> <p>After the above server receives a connection, the very first <code>select()</code> on the connection exits without blocking and there are no keys with ready operations. I don't know why Java behaves in this way, but it appears many people get bitten by this behavior.</p> <p>The outcome is the same on Sun's JVM 1.5.0_06 on Windows XP as well as Sun's JVM 1.5.0_05 and 1.4.2_04 on Linux 2.6.</p>
 

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