【发布时间】:2025-12-16 00:05:01
【问题描述】:
我想将 C# 客户端应用程序与 Java 服务器应用程序通信。 Java 使用带有 protobuf 管道编码器的 Netty 框架。
我的原型文件: 导入“google/protobuf/csharp_options.proto”;
option (google.protobuf.csharp_file_options).namespace = "ChatClient.LoginProtocol";
option (google.protobuf.csharp_file_options).umbrella_classname = "LoginProtocol";
option optimize_for = SPEED;
message Credential {
required string email = 1;
required string password = 2;
}
Netty 管道编解码器:
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(LoginProtos.Credential.getDefaultInstance()));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
那么我怎样才能将消息发送到 java 服务器。 c#代码:
stream = client.GetStream();
sReader = new StreamReader(stream);
sWriter = new StreamWriter(stream);
Credential.Builder builder = Credential.CreateBuilder();
builder.SetEmail("xxx@gmail.com").SetPassword("12356");
sWriter.Write(builder.Build().ToByteArray());
sWriter.Flush();
感谢您的帮助,对不起我的英语。
【问题讨论】:
-
我可以整天谈论 protobuf 编码,但是 - 我不熟悉这里的 Netty 部分。如果您能指出定义 Netty 协议定义的位置(不用担心 protobuf),我可能会提供帮助
-
在 java 服务器上,我们读取了类似“Credential cr = (Credential) e.getMessage();”的消息如果我将 java 客户端与 protobuf 一起使用,则没有问题。但是如果我使用 C# 我无法阅读。 Netty 协议在上面。在我阅读之前,它会解码消息。如果您不熟悉 Netty,我认为很难解释它是如何为我工作的。
-
@user740668 它可能不是很复杂,但是...
标签: c# java protocol-buffers netty