有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

Netty服务器可以从DontNety客户端接收信息 但是,DontNetty客户端无法从Netty服务器接收信息

它们使用protobuf序列化对象

客户

   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) 个答案

  1. # 1 楼答案

    我还没有与DotNetty合作过,但我猜你添加了一个只读取BaseMsgCSimpleChannelInboundHandler,而不是将IByteBuffer解码为BaseMsgC。有两种方法可以解决这个问题

    1. SimpleChannelInboundHandler中的泛型更改为IByteBuffer
    2. 更改处理程序管道,使其能够将IByteBuffer解码为BaseMsgC

    顺便说一句,也许这很有帮助DotNetty.Codecs.Protobuf