return;
}
// 注册Read事件
ioEvent.attachment().getSelectorHandler().register(
k, SelectionKey.OP_READ);
} finally {
if (waitLatch != null) {
waitLatch.countDown();
}
}
}
public void onRead(IOEvent
ByteBuffer bb = ByteBuffer.allocate(100);
try {
tch.read(bb, false);
} catch (IOException e) {
e.printStackTrace();
}
bb.flip();
System.out.println(new String(bb.array(), 0, bb
.remaining()));
// 重新注册read事件,原因参考TcpSelectorHandler的onReadInterest/onWriteInterest方法
ioEvent.attachment().getSelectionKey().interestOps(
k.interestOps() | SelectionKey.OP_READ);
}
public void onWrite(IOEvent
}
});
System.out.println("current connection:" + tch.getUnderlyingChannel());
try {
waitLatch.await();
} catch (Exception e) {
// do nothing
}
ByteBuffer bb = ByteBuffer.wrap("xxxx".getBytes());
tch.write(bb, true);
}
}
本文出自 “天下无贼” 博客