ReadRawVarint32() 的问题 - Google Protocol Buffers csharp-port

发布于 2024-12-13 10:16:12 字数 1155 浏览 0 评论 0原文

我正在尝试使用 Google Protocol Buffers(具体来说,Jon Skeet 的 csharp-port)从服务器中的客户端接收一些数据。我执行以下操作:

using Google.ProtocolBuffers;
...
Stream InputStream = client.GetStream();
CodedInputStream input = CodedInputStream.CreateInstance(InputStream);
...
uint length = CodedInputStream.ReadRawVarint32(InputStream);

我从最后一行收到一条无法解决的错误消息:需要对象引用才能访问非静态成员“Google.ProtocolBuffers.CodedInputStream.ReadRawVarint32()”。

基本上我想做的在java版本中是这样的:

InputStream iStream = client.getInputStream();
CodedInputStream input = CodedInputStream.newInstance(iStream);
int read = is.read();
if(-1 != read) {
    int length = CodedInputStream.readrawVarint32(read, is);
    byte[] bytes = input.readRawBytes(length);
    // My proto stuff
    Communication.Packet container = null;
    try {
        container = Communication.Packet.parseFrom(bytes);
    } catch (InvalidProtocolBufferException iPBE) {
        continue;
    }
    AbstractMessage message = container;
    if(container.hasLogin()) {
        message = container.getLogin();
    }
    System.out.println(message.toString());

有帮助吗?

提前致谢。

I'm trying to receive some data from a client in a server using Google Protocol Buffers, concretely, Jon Skeet's csharp-port. I do the following:

using Google.ProtocolBuffers;
...
Stream InputStream = client.GetStream();
CodedInputStream input = CodedInputStream.CreateInstance(InputStream);
...
uint length = CodedInputStream.ReadRawVarint32(InputStream);

I get an error message from the last line which i cannot solve: An object reference is requiered to access non-static member 'Google.ProtocolBuffers.CodedInputStream.ReadRawVarint32()'.

Basicly what i want to do would be like this in the java version:

InputStream iStream = client.getInputStream();
CodedInputStream input = CodedInputStream.newInstance(iStream);
int read = is.read();
if(-1 != read) {
    int length = CodedInputStream.readrawVarint32(read, is);
    byte[] bytes = input.readRawBytes(length);
    // My proto stuff
    Communication.Packet container = null;
    try {
        container = Communication.Packet.parseFrom(bytes);
    } catch (InvalidProtocolBufferException iPBE) {
        continue;
    }
    AbstractMessage message = container;
    if(container.hasLogin()) {
        message = container.getLogin();
    }
    System.out.println(message.toString());

Any help?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-12-20 10:16:12

该错误消息表明您正在尝试访问非静态(即成员方法)而不使用对象引用。您需要更改方法调用以对 CodedInputStream 类型的对象而不是 CodedInputStream 类进行操作:

input.ReadRawVarint32();

The error message states that you are trying to access a non-static (i.e. a member method) without using an object reference. You need to change your method call to operate on the object of type CodedInputStream instead of the CodedInputStream class:

input.ReadRawVarint32();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文