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

时间:2014-10-06 18:53:33

标签: java tcp netty

我尝试从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());

                   }
        }); 

但在ServerHandler课程中,我无法找到并调用该方法:

void channelRead (ChannelHandlerContext ctx, byte[] bytes){}

而不是典型的:

void channelRead (ChannelHandlerContext ctx, Object msg){}

Thnx提前!

1 个答案:

答案 0 :(得分:0)

刚做演员......

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