Java 使用netty从tcp客户端读取二进制数据

Java 使用netty从tcp客户端读取二进制数据,java,tcp,netty,Java,Tcp,Netty,我正在尝试从tcp客户端应用程序获取字节数据,根据netty项目的文档,我找到了这段代码,并将其实现到我的系统中: ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true)

我正在尝试从tcp客户端应用程序获取字节数据,根据netty项目的文档,我找到了这段代码,并将其实现到我的系统中:

ServerBootstrap b = new ServerBootstrap();
      b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_KEEPALIVE, true)
            .handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new ChannelInitializer<SocketChannel>() {

                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                   ChannelPipeline p = ch.pipeline();
                   p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
                   p.addLast("bytesDecoder", new ByteArrayDecoder());
                   p.addLast(new ServerHandler());

                   }
        }); 
而不是典型的:

void channelRead (ChannelHandlerContext ctx, Object msg){}
提前通知

只要做演员就行了

void channelRead (ChannelHandlerContext ctx, Object msg){
    byte[] bytes = (byte[]) msg;
}

好的,谢谢你,但是我为什么需要向管道中添加处理程序
p.addLast(“frameDecoder”,new LengthFieldBasedFrameDecoder(1048576,0,4,0,4))
p.addLast(“bytesDecoder”,新的ByteArrayCoder())??如果只是做一个cast?因为否则您将不会收到一个byte[],而是一个bytebuffer。感谢您的回答和开发Netty是一个了不起的框架:)
void channelRead (ChannelHandlerContext ctx, Object msg){
    byte[] bytes = (byte[]) msg;
}