Code example for SelectionKey

Methods: cancel, channel

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