Code example for SelectionKey

Methods: cancel, channel

0
	private void readOperation(SelectionKey key) throws Exception {
		session.setSelecotr(selector);
		session.setKey(key);
		session.setIsServerChannel(false);
		 
		SocketChannel channel = (SocketChannel) key.channel();
		ByteBuffer dst = ByteBuffer.allocate(1024);
		int n = -1;
		try { 
			n = channel.read(dst);
		} catch (Exception e) {
			System.err.println("cannot read io");
		} 
		if (n == -1) {
			key.cancel();
			channel.close();
			return; 
		} 
		dst.flip();
		 
		handler.messageReceive(session, dst);