写NIO程序的时候遇到了SelectionKey.attachment()为空的问题,最后解决了,这里简单介绍下,希望能对其他人有帮助:
SelectionKey key = socketChannel.register(selector, SelectionKey.OP_CONNECT);
key.attach(group);
...
client.register(selector, SelectionKey.OP_READ);
if(key.isReadable()){
ChannelWorkerGroup group = (ChannelWorkerGroup) key.attachment();
的时候却报group为空。
原因是第二次register为OP_READ的时候没有带上第三个参数,改成这样就好了:
client.register(selector, SelectionKey.OP_READ, group);
if(key.isReadable()){
ChannelWorkerGroup group = (ChannelWorkerGroup) key.attachment();
分享到:
相关推荐
if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) { // 新生注册 ServerSocketChannel serverChanel = (ServerSocketChannel) key.channel(); SocketChannel sc = serverChanel....
client.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); while (true) { selector.select(); for (SelectionKey key : selector.selectedKeys()) { if (key.isConnectable()) { // 连接...
SelectionKey acceptKey = serverSocket.register(selector, SelectionKey.OP_ACCEPT); ``` 客户端则使用`Socket`类建立连接到服务器的通道: ```java Socket socket = new Socket("localhost", 8888); ``` 在...
这个新的`SocketChannel`也需要注册到选择器,但这次我们关注的是读写操作(`SelectionKey.OP_READ`或`SelectionKey.OP_WRITE`)。 6. **读取和写入数据**: 当`SocketChannel`准备好读取数据时,调用`read()`方法...
3.可以使用或运算符(|)组合多个事件,例如,SelectionKey.OP_READ | SelectionKey.OP_WRITE。 Selector 的工作过程可以分为以下几步: 1.创建一个 Selector 对象:Selector selector = Selector.open(); 2.将 ...
即需要采用SelectionKey.cancel()从注册的Selector中取消对该Selection的监视,防止同时多个线程获取到SelectionKey的事件 2.注册在向Selector注册通道的时候,如果register方法抛出KeyCancelledException表明,...
clientSocket.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); ``` 8. **读写数据**:在选择器的事件循环中,处理读写事件。对于读事件,使用`ByteBuffer`读取SocketChannel的数据;对于写事件...
s = "Att: " + (sk.attachment() == null ? "no" : "yes"); s += ", Read: " + sk.isReadable(); s += ", Acpt: " + sk.isAcceptable(); s += ", Cnct: " + sk.isConnectable(); s += ", Wrt: " + sk.is...
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); ``` 4. **轮询并处理事件**: ```java while (true) { int numReady = selector.select(); if (numReady > 0) { Set<SelectionKey> keys ...
serverSocket.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); for (SelectionKey key : selector.selectedKeys()) { if (key.isAcceptable()) { ServerSocketChannel ssc =...
channel.register(selector, SelectionKey.OP_CONNECT); } /** * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 * @throws IOException * @throws InterruptedException */...
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); // 非阻塞,等待事件发生 Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); while ...
serverChannel.register(selector, SelectionKey.OP_ACCEPT); ``` 对于客户端: ```java SocketChannel clientChannel = SocketChannel.open(); clientChannel.configureBlocking(false); clientChannel.register...
SelectionKey key = serverSocket.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); for (SelectionKey selectedKey : selector.selectedKeys()) { if (selectedKey....
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); while (keys.hasNext()) { ...
ssc.register(selector, SelectionKey.OP_ACCEPT); // 循环等待选择器返回可操作的通道 while (true) { // 选择就绪的通道 selector.select(); // 获取并迭代所有已选择的键 Iterator<SelectionKey> it = ...
server.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); while (keys.hasNext()) { SelectionKey key = ...
server.register(selector, SelectionKey.OP_ACCEPT); ``` 将ServerSocketChannel注册到Selector上,并指定监听accept事件,即客户端连接请求。 7. **循环监听**: 使用一个循环结构,持续监听Selector上的事件...
ssc.register(selector, SelectionKey.OP_ACCEPT); while (true) { // 轮询选择器,获取准备就绪的通道 int nKeys = selector.select(); if (nKeys > 0) { // 处理准备就绪的通道 Set<SelectionKey> ...
socketChannel.register(selector, SelectionKey.OP_CONNECT); while (true) { // 选择准备就绪的事件 selector.select(); // 获取所有已选择的键 Iterator<SelectionKey> keyIterator = selector....