如何修复C#DontNetty客户端无法从Java Netty i接收

时间:2017-10-23 01:23:37

标签: java c# netty

Netty Server可以从DontNetyy Client收到信息 但是,DontNetty客户端无法从Netty Server接收信息

他们使用protobuf序列化对象

c#c​​lient

   var bootstrap = new Bootstrap();
            bootstrap
                .Group(group)
                .Channel<TcpSocketChannel>()
                .Option(ChannelOption.TcpNodelay, true)
                .Option(ChannelOption.SoKeepalive,true)
                .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;



                    pipeline.AddLast(new ProtobufVarint32LengthFieldPrepender());

                    pipeline.AddLast(new ProtobufVarint32FrameDecoder());
                    pipeline.AddLast(new EchoClientHandler());
                }));

            IChannel clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port));

java服务器

    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            ChannelPipeline p = socketChannel.pipeline();


            p.addLast(new ProtobufVarint32FrameDecoder());

            p.addLast(MessageCodec.PROTO_DECODER, new ProtobufDecoder(BaseMsgOuterClass.BaseMsg.getDefaultInstance()));



            p.addLast(new ProtobufVarint32LengthFieldPrepender());
            p.addLast(new ProtobufEncoder());


            p.addLast(serverHandler);
        }
    });

服务器向客户发送信息。 但是没有调用c#client read0方法。

public class EchoClientHandler : SimpleChannelInboundHandler<BaseMsgC>
{


    protected override void ChannelRead0(IChannelHandlerContext ctx, BaseMsgC msg)
    {
        Console.Write("ChannelRead0 is not called ");
        Console.Write(msg.Msg);
        Console.Write(msg.MsgType);
        throw new NotImplementedException();
    }
    public override void ChannelRead(IChannelHandlerContext context, object message)
    {

        Console.Write("ChannelRead is called");
    }     
}

}

1 个答案:

答案 0 :(得分:0)

我还没有与DotNetty合作,但我的猜测是你添加SimpleChannelInboundHandler只读BaseMsgC并且你没有将IByteBuffer解码为{ {1}}。有两种方法可以解决这个问题。

  1. BaseMsgC中的通用更改为SimpleChannelInboundHandler
  2. 更改处理程序管道,以便能够将IByteBuffer解码为IByteBuffer
  3. PS。也许这是有帮助的DotNetty.Codecs.Protobuf