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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:46:42+00:00 2026-05-26T12:46:42+00:00

I am trying to implement a (T)LV protocol over TCP sending protocol buffers from

  • 0

I am trying to implement a (T)LV protocol over TCP sending protocol buffers from a python client and receiving with a C++ server.

My code looks more or less like this:

char* buffer[RCVBUFSIZE];
int recvsize= 0;
// Filling my buffer with as much as possible.
while(true) {
  if(recvsize == RCVBUFSIZE) {
    break;
  } else if(recvsize+= recv(sock, buffer+recvsize, sizeof(buffer)-recvsize, 0) < 1) {
    break;
  }
}
//Parsing LV protocol
while(true) {
  unsigned short protosize= 0;
  //Copy first two bytes into protosize
  memcpy((char *) &protosize, buffer, sizeof(unsigned short));
  if(protosize == 0) { break; } // Protocol indicates EOM be setting length to "0"
  void* protomsg[protosize];
  memcpy(protomsg, buffer+sizeof(unsigned short), protosize);
  int msglength= sizeof(unsigned short)+protosize;
  //Now I'll move the whole buffer to the left so that I don't have to keep track of where I'm at.
  memmove(buffer, buffer+msglength, RCVBUFSIZE-msglength);
  protoMsg pm;
  if(!pm.ParseFromArray(protomsg, protosize)) { break; } // Parsing failed.
  // Do stuff with parsed message.
}

Now I have several problems:

  • The while loop receiving the message never terminates. I suspect that the recv call blocks when there isn’t any data left anymore while I expected it to return with an error. I have found the select function to check whether there’s something to read. I will give that a try.
    But when I call receive only once to skip this problem (The message received comes in at ~10 bytes, so I expect all to be collected in one call.) I get another problem:
  • memcpy and memmove don’t seem to be working as expected. On the first loop the short is processed as expected (I receive the same value I send on the other side), but then everything from parsing of the protocol buffer fails. Have I misunderstood something?

Edit: Regarding the comment about ntohs — I’m transmitting the short as little-endian currently, forgot to mention that. (I will change this still, btw.)

Third edit: The code now works, but I had to change the following:

char* buffer[RCVBUFSIZE];
int recvsize= 0;
// Filling my buffer with as much as possible.
while(true) {
  if(recvsize == RCVBUFSIZE) {
    break;
  } else if((recvsize+= recv(sock, buffer+recvsize, sizeof(buffer)-recvsize, 0)) < 1) {
    break;
  } else if(recvsize > 1) {
    unsigned short msglength= 0;
    memcpy((char *) &msglength, buffer+recvsize-sizeof(unsigned short), sizeof(unsigned short));
    if(msglength == 0) { break; } // Received a full transmission.
  }
}

So first I needed to add brackets around the recvsize+= recv() statement and second as the non-blocking method didn’t work for some reason I am now checking whether the last two bytes that were transmitted translate to a 0 when read a unsigned short. This probably leads to a problem if I read a 0 by chance that is not the length field. I’ll start another question about this probably.

I also changed protomsg to char[], but I don’t think this really changed anything. (I had parsing working with a void array already.. )

  • 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-26T12:46:42+00:00Added an answer on May 26, 2026 at 12:46 pm

    If the message you receive is always around 10 bytes, and RCVBUFSIZE is more than that, you will never terminate until there is a error reading data. Also, the buffer variable in your code is an array of RCVBUFSIZE pointers, probably not what you want.

    Modify your code as follows:

    #define MINIMALMESSAGESIZE 10  // Or what the size may be
    char buffer[RCVBUFSIZE];
    int totalrecvsize= 0;
    int recvsize= 0;
    while(true) {
      if(totalrecvsize >= MINIMALMESSAGESIZE) {
        break;
      } else if(recvsize= recv(sock, buffer+totalrecvsize, sizeof(buffer)-totalrecvsize, 0) < 1) {
        break;
      } else {
        totalrecvsize += recvsize;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a minimal chat server in java over regular TCP
I'm trying to implement this protocol: http://en.wikipedia.org/wiki/Chord_(peer-to-peer ) What i understood from it is
I'm trying to implement a simple FTP server (a variation of the EFTP protocol)
I am working from this reference , and trying to implement the OAuth protocol
I'm trying to implement such a protocol: Client side: 1) client sends command (String)
Am trying to implement a generic way for reading sections from a config file.
I am trying to implement string unescaping with Python regex and backreferences, and it
I came from a java background, and I was trying to use protocol like
I am trying to implement a REST protocol and have realized in trying to
I'm doing a study on WebSocket protocol and trying to implement a simple ECHO

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.