【问题标题】:netty 4.0 input bytes decodingnetty 4.0 输入字节解码
【发布时间】:2013-03-13 05:33:49
【问题描述】:

我正在尝试实现一个非常简单的套接字服务器。我需要阅读一些消息,用新行或任何其他分隔符分隔。

根据本文档:http://netty.io/4.0/api/io/netty/handler/codec/string/StringDecoder.htmlhttp://netty.io/4.0/api/io/netty/handler/codec/DelimiterBasedFrameDecoder.html

通道初始化器的代码

ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {

                ch.pipeline().addLast("frameDecoder", new DelimiterBasedFrameDecoder(Delimiters.lineDelimiter()));
                ch.pipeline().addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));

                // Encoder
                ch.pipeline().addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
            }
         };

应该可以解决问题。

但是对于任何可以使用提供的参数的解码器和编码器都没有合适的构造函数。他们期望一些整数作为第一个参数和一个字节数组。只有 stringDecoder 似乎没问题。

我在这里错过了什么?

【问题讨论】:

  • 对不起,我不明白这个问题和问题。你能改写一下吗?
  • 简而言之:从文档中获取的使用示例不起作用。那就是问题所在。问题是如何正确使用它

标签: java sockets network-programming netty pipeline


【解决方案1】:

netty 4.0 的内联文档目前已经过时——但它处于 alpha/beta/development 状态。

引入整数参数是为了防止缓冲区无限增长并指定最大帧长度。

所以new LineBasedFrameDecoder(4096)) 类似于new DelimiterBasedFrameDecoder(Delimiters.lineDelimiter())

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    相关资源
    最近更新 更多