Netty Server可以从DontNetyy Client收到信息 但是,DontNetty客户端无法从Netty Server接收信息
他们使用protobuf序列化对象
c#client
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");
}
}
}
答案 0 :(得分:0)
我还没有与DotNetty合作,但我的猜测是你添加SimpleChannelInboundHandler
只读BaseMsgC
并且你没有将IByteBuffer
解码为{ {1}}。有两种方法可以解决这个问题。
BaseMsgC
中的通用更改为SimpleChannelInboundHandler
或IByteBuffer
解码为IByteBuffer
PS。也许这是有帮助的DotNetty.Codecs.Protobuf