问答详情
源自:4-5 功能完善

socketChannel多次注册疑问

请问一下在处理readHandler方法中为什么要把socketChannel再次注册到selector上,不是已经注册过了么


String str = "";
//读取的字节大于0
while(channel.read(buffer) > 0){
    //将buffer转换为读模式
    buffer.flip();

    str += Charset.forName("UTF-8").decode(buffer);
}

//将channel再次注册到selector上
channel.register(selector,SelectionKey.OP_READ);

if(str.length() > 0){
    //广播给其它客户端
    broadcast(selector,channel,str);
}


提问者:weixin_慕妹427387 2021-07-13 20:58

个回答

  • qq_慕九州5564661
    2021-12-28 15:41:05

    123456