Code example for SelectionKey

Methods: channel

0
            buf.clear();
            return true; 
        } 
 
        private void acceptConnection(SelectionKey key) throws IOException {
            ServerSocketChannel channel = (ServerSocketChannel) key.channel();
            SocketChannel client = channel.accept();
            client.configureBlocking(false);
 
            Tag t = new Tag();
            t.buf = ByteBuffer.allocateDirect(512);
            t.client = true;
 
            SelectionKey clientKey = client.register(mSelector, SelectionKey.OP_READ);
            clientKey.attach(t);
            Log.i(TAG, "Client connected: " + client.socket().getRemoteSocketAddress());
        } 
 
        private Selector createSelector() throws IOException {
            ServerSocketChannel serverSocket = ServerSocketChannel.open();
            serverSocket.socket().setReuseAddress(true);