Netty + Goole Protobuf报错:Protocol message contained an invalid tag (zero).

参考文章:http://www.starmcu.com/archives/645

在客户端和服务端的Initializer里要注意顺序:
ProtobufEncoder要放在ProtobufVarint32LengthFieldPrepender后面。

public class MySixthClientInitializer extends ChannelInitializer{
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline channelPipeline = socketChannel.pipeline();

        channelPipeline.addLast(new ProtobufVarint32FrameDecoder());

        channelPipeline.addLast(new ProtobufDecoder(DataInfo.MyMessage.getDefaultInstance()));
        channelPipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
        channelPipeline.addLast(new ProtobufEncoder());
        channelPipeline.addLast(new MySixthClientHandler());
    }
}
public class MySixthServerInitializer extends ChannelInitializer{

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline channelPipeline = socketChannel.pipeline();

        channelPipeline.addLast(new ProtobufVarint32FrameDecoder());
        channelPipeline.addLast(new ProtobufDecoder(DataInfo.MyMessage.getDefaultInstance()));
        channelPipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
        //channelPipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
        channelPipeline.addLast(new ProtobufEncoder());

        channelPipeline.addLast(new MySixthServerHandler());
    }
}

你可能感兴趣的:(Netty)