【发布时间】:2013-03-13 05:33:49
【问题描述】:
我正在尝试实现一个非常简单的套接字服务器。我需要阅读一些消息,用新行或任何其他分隔符分隔。
根据本文档:http://netty.io/4.0/api/io/netty/handler/codec/string/StringDecoder.html 和 http://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