2014-10-06 71 views
0

TCP客户端我试图从TCP客户端应用程序中获取字节的数据,网状项目的下列文件二进制数据,我发现这个代码,我实现它到我的系统:阅读使用网状

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提前!

回答

0

只是做投...

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

好的,谢谢你,但为了什么原因,我需要添加到管道中的处理程序'p.addLast( “frameDecoder”,新LengthFieldBasedFrameDecoder(1048576,0,4 ,0,4));'和 'p.addLast(“bytesDecoder”,new ByteArrayDecoder());''如果只是做一个演员? – HCarrasko 2014-10-07 12:05:53

+0

,否则你将不会收到一个字节[],但一个ByteBuffer – 2014-10-08 04:50:09

+0

感谢您的答案和开发Netty是一个了不起的框架:) – HCarrasko 2014-10-08 05:10:19