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 6255141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:18:34+00:00 2026-05-24T14:18:34+00:00

Sorry, but I think I am going mad. I have this in C++: std::stringstream

  • 0

Sorry, but I think I am going mad. I have this in C++:

std::stringstream message;
protoMsg.SerializeToOstream(&message);
boost::system::error_code ignored_error;
std::cout << "Writing response to socket. Byte size " << protoMsg.ByteSize() << " Buffer " << message.str().length() << std::endl;
for(int i(0);i<(int)message.str().length();i++)
    std::cout << (int)message.str().at(i);
std::cout << std::endl;
boost::asio::write(socket, boost::asio::buffer(message.str()), ignored_error);
std::cout << "Done writing." << std::endl;

Which yields this output

Writing response to socket. Byte size 88 Buffer 88
1886811891161111001113278971091012500000000320400480560640730000000081000000008859621061211611110011132115121109789710910111416691201161011141109710832114101113117101115116
Done writing.

And this in Java:

try {
  System.out.println("Parsing");
  int lenbytes = is.read(buffer);
  System.out.println("Read bytes " + lenbytes);
  for(int i=0;i<lenbytes;i++)
    System.out.print(Integer.toString((int)buffer[i]));
  System.out.println("");
  EnvInfoProto envInfoProto = EnvInfoProto.parseFrom(buffer);
  System.out.println("Done");
  return envInfoProto;
} catch(IOException ignore) { System.out.println("Ex: " + ignore.toString(); }

Which yields

Parsing
Read bytes 88
1886811891161111001113278971091012500000000320400480560640730000000081000000008859621061211611110011132115121109789710910111416691201161011141109710832114101113117101115116
Ex: com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).

The binary data is the same. I checked that I am using the right version of the proto files. I am a bit at a loss tbh. Any help appreciated.

  • 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-24T14:18:35+00:00Added an answer on May 24, 2026 at 2:18 pm

    You’re asking the message to be parsed from the whole of buffer – and my guess is that buffer is more than 88 bytes long.

    I can’t remember offhand whether parseFrom allows you to specify the maximum amount of data to read, but an alternative would be:

    ByteArrayIntputStream stream = new ByteArrayInputStream(buffer, 0, lenbytes);
    EnvInfoProto envInfoProto = EnvInfoProto.parseFrom(stream);
    

    Note that this still has the problem that you’re assuming you can read all the data from the stream in a single call to read, which is never a good idea – but that’s a very separate issue. If you’re going to close the socket after writing, you could just parse stream from the socket’s InputStream in the Java code, of course. If you’re not going to close the stream, I would suggest writing the message length to the socket first (as a 32-bit integer) so you can then read that in Java, and read exactly the right amount of data, knowing when you’re finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the vague subject but I couldn't think what to put. Here's my
Sorry for the bad title, but I couldn't think of a better one. I'm
sorry but I do not have the actual code with me, but I will
Sorry for this not being a real question, but Sometime back i remember seeing
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry if this sounds like a really stupid question, but I need to make
I'm sorry, but this keyword doesn't mean anything to me... Can someone give me
Sorry for the simple question but I feel like there's a smarter way to
Sorry for my ignorance here, but when I hear the word webserver, I immediately
Sorry if the title is poorly descriptive, but I can't do better right now

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.