2014-10-06 72 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