Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:01:04+00:00 2026-05-22T23:01:04+00:00

I am going to read messages that are stored consecutively in socket in C++

  • 0

I am going to read messages that are stored consecutively in socket in C++ client that are sent from a C# server. I expect that I can read the size of a message like that:

google::protobuf::uint32 m;
coded_input->ReadVarint32(&m);
cout << m << endl;

Then I want to read the message:

Person person;
CodedInputStream::Limit limit = coded_input->PushLimit(m);
person.ParseFromCodedStream(coded_input);
coded_input->PopLimit(limit);

the C# code looks like:

var person = new Person { Id = 123456, Name = "Fred", Address = new Address { Line1 = "Flat 1", Line2 = "The Meadows ar garą " } };
Stream str = new NetworkStream(socket);
Serializer.SerializeWithLengthPrefix(str, person, PrefixStyle.Fixed32);

It doesn’t work.

I get the C++ error (43 is result of cout << m << endl;) (id, name, address are all fields in Person)

43

libprotobuf ERROR
google/protobuf/message_lite.cc:123]
Can’t parse message of type “Person”
because it is missing required fields:
id, name, address

When I don’t read the variant and parse the message straighforward from coded_input, all is fine (when I change SerializeWithLengthPrefix to serialize in server’s code). However I need a method to distinguish consecutive messages, so I need to know the size of the message I’m going to read. I just don’t know how to send the size.

I tried:

Stream buf = new MemoryStream();
Serializer.Serialize(buf, person);
ProtoWriter writer = new ProtoWriter(str, null);
ProtoWriter.WriteInt32((int)buf.Length, writer);

but then I get:

Unhandled Exception:
ProtoBuf.ProtoException: Invalid
serialization operation with wire-type
None at position 0 at
ProtoBuf.ProtoWriter.WriteInt32 (Int32
value, ProtoBuf.ProtoWriter writer)
[0x00000] in :0
at protobuftest.MainClass.Main
(System.String[] args) [0x00097] in
/home/lorddidger/studia/csharp/protobuf-test/protobuf-test/Main.cs:31

I am not able to send any int that way. What is wrong?


Update:
Actually, my aim is to find a way to transfer an integer (size) with protobuf. I need any example (both C++ and C#) how to handle that because I don’t understand all details within protobuf.

I noticed that SerializeWithLengthPrefix send prefix (4 uint32) that look like: size 0 0 0. The size is number of bytes of the message after serialization (I guess). I thought PrefixStyle.Fixed32 says there is only one uint32 before the message but there are 4!

Finally, I thought you should use ProtoWriter.WriteInt32((int)buf.Length, writer) to transfer integers because I followed suggestion from somwhere in the internet which I suppose is a workaround relevant to C++. Now I see you shouldn’t write varints – they are related to the engine and that is to sophisticated to spend time on.

Should I send a message with an int? How should I distinguish that from next message that size is stored in the first one?

I see C# can hadle prefixes fine but is there ANY COMPATIBLE with C++ and C# way to state size of a message? Otherwise, there is no **ing way to queue messages what I would find irrational.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:01:05+00:00Added an answer on May 22, 2026 at 11:01 pm

    Your first example includes just a length; the “with length prefix” actually encodes in a protobuf-compatible stream.

    If you are decoding from c++, read two varints; the first is the field-number and wire-type; the second is the length. The first is packed as 3 bits wire-type, the rest is the field-number. You might also be able to specify field 0 to skip – I can’t recall without checking.


    Update; I checked this out, writing the data without a field-number (just a varint length prefix), and then reading it back in C# using two different APIs – individually with Deserialize, and as an enumerable block via DeserializeItems. I had to fix a bug in the latter, but the following will work from the next code push (note: only the reading code has a fix, so if you are writing in C# and reading in C++ this won’t affect you):

    using (var ms = new MemoryStream())
    {
        // write data with a length-prefix but no field number
        Serializer.SerializeWithLengthPrefix(ms, new Foo { Bar = 1 }, PrefixStyle.Base128, 0);
        Serializer.SerializeWithLengthPrefix(ms, new Foo { Bar = 2 }, PrefixStyle.Base128, 0);
        Serializer.SerializeWithLengthPrefix(ms, new Foo { Bar = 3 }, PrefixStyle.Base128, 0);
    
        ms.Position = 0;
        Assert.AreEqual(9, ms.Length, "3 lengths, 3 headers, 3 values");
    
        // read the length prefix and use that to limit each call
        TypeModel model = RuntimeTypeModel.Default;
        int len, fieldNumber, bytesRead;
        List<Foo> foos = new List<Foo>();
        do
        {
            len = ProtoReader.ReadLengthPrefix(ms, false, PrefixStyle.Base128, out fieldNumber, out bytesRead);
            if (bytesRead <= 0) continue;
    
            foos.Add((Foo)model.Deserialize(ms, null, typeof(Foo), len));
    
            Assert.IsTrue(foos.Count <= 3, "too much data!");
        } while (bytesRead > 0);
    
        Assert.AreEqual(3, foos.Count);
        Assert.AreEqual(1, foos[0].Bar);
        Assert.AreEqual(2, foos[1].Bar);
        Assert.AreEqual(3, foos[2].Bar);
    
        // do it using DeserializeItems
        ms.Position = 0;
    
        foos.Clear();
        foreach (var obj in model.DeserializeItems<Foo>(ms, PrefixStyle.Base128, 0))
        {
            foos.Add(obj);
            Assert.IsTrue(foos.Count <= 3, "too much data!");
        }
        Assert.AreEqual(3, foos.Count);
        Assert.AreEqual(1, foos[0].Bar);
        Assert.AreEqual(2, foos[1].Bar);
        Assert.AreEqual(3, foos[2].Bar);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# server that accepts multiple clients, and multiple messages from each
First I've read loads of posts and sites that recommend going to http://silverlight.net/GetStarted/ to
Is it okay to get a read-only collection from an aggregate without going through
I have created a simple chat server that is driven by client polling. Clients
I'm using a Socket to communicate with a ServerSocket. Strings are being sent from
I thought I had read somewhere that Silverlight 4 was going to contain a
I've got a bunch of properties which I am going to use read/write locks
I'm getting something pretty strange going on when trying to read some data using
I need to read a BLOB and store it in a byte[], before going
Going from the example given here... http://ericswann.org/blog/archive/2009/04/06/linq-to-sql-datacontext-provider-revisited.aspx I'm trying to use the datacontext between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.