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

The Archive Base Latest Questions

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

I need to send a number of complex objects over the network to a

  • 0

I need to send a number of complex objects over the network to a peer. I have written the code to serialize them using ostream and the operator<< for each class member in the objects that need to be serialized. The code I have written works successfully for serialization and network sending (htonl(), htons() etc. correctly used–I checked the aforementioned by writing to an ofstream (local file) in binary format (std::ios::bin). My next task, writing this binary data over the network socket, is where I am having issues.

I have Socket class which translates std::string objects into C-style strings before sending them over the socket like so:

int Socket::send (const std::string goodies) const
{
    status = ::send (socket, goodies.c_str(), goodies.size(), 0);
            return status;
}

the same Socket class, which I use in the receiver, uses the recv() to place the incoming message into a std::string before passing it to the deserializing application:

int Socket::recv (std::string& goodies)
{
    char buf [1024];
    goodies = "";
    memset (buf, 0, 1025);

    int status = ::recv (socket, buf, 1024, 0);

    if (status < 0)
    {
        return -1;
    }
    else if (status == 0)
    {
        return 0;
    }
    else
    {
        goodies = buf;
        return status;
    }
}

I do the sending using the following code:

ostringstream os (std::ios::binary);
GiantObject giantComplexObjectWithWholeLoadOfOtherObjects;
// Initialize and set up 
// giantComplexObjectWithWholeLoadOfOtherObjects.

// The following call works well--I tested it by writing to ofstream locally (file)
// and checked it out using a hex dump.
// Now, of course, my intent is to send it over the network, so I write to
// ostream&:
giantComplexObjectWithWholeLoadOfOtherObjects.serialize (os);

std::string someStr = os.str(); // Get me the stream in std::string format

mySocket.send(someStr); // Does not work--send sent correctly, but recv received 0 bytes

However, if I try:

std::string someStr ("Some silly string");
mySocket.send (someStr); // received by receiver (receiver is similar arch).

Thereby, something is not right about my call to send binary std::string to the socket. Any help is greatly appreciated. Once again, I do not want to use Boost, protobuf etc.

PS: I have spent a considerable amount of time looking over the old posts here, and the first response these types of questions receive is to use Boost. Please–there are other non-Boost, non-Protobuf ways, and I want to understand those other ways. I do appreciate what Boost and Protobuf bring to the table, I just want to do this in the manner I have set up the code.

  • 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-26T04:49:46+00:00Added an answer on May 26, 2026 at 4:49 am

    I believe one problem is the use of std::string as your container. Especially the line goodies = buf;. The std::string assignment operator from a char* assumes that the input ends at the first NUL. Since binary data can contain arbritrary NUL characters, it will likely terminate earlier than you expect.

    For an application such as this, I recommend the use of std::vector<char> or std::vector<unsigned char>. Since the memory in the buffer is always contiguous, you can access the underlying buffer by taking the address of the first element.

    For the receive side, you will want to use std::copy to copy the data. Or you can set it up to use a vector buffer, and swap once the receive is complete. This assumes that you want goodies unmodified if the status is <= 0. If you can accept goodies being changed even in those cases, you can resize it prior to calling ::recv(), and avoid creating the extra vector.

    int Socket::send (const std::vector<char>& goodies) const {
        status = ::send (socket, &goodies[0], goodies.size(), 0);            
        return status; 
    
    int Socket::recv (std::vector<char>& goodies)  {    
      std::vector<char> buffer(1024);
      int status = ::recv (socket, &buf[0], buf.size());
      if (status < 0) 
      {
         return -1;      
      }
      else if (status == 0)
      { 
         return 0;
      }
      else
      {
         buffer.resize(status);
         goodies.swap(buffer);
         return status;      
      } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to send a message to a number of people to
I have a user table with date of birth. I need to send them
I need to send floating point numbers using a UDP connection to a Qt
I need to send and receive data over serial connections (RS-232 and RS-422). How
I need to send MMS thought a C# application. I have already found 2
I need to send a number of emails out from my RoR application, and
I need to send email notifications in my Java web application. I'm using Apache
I have been happy serializing with javascript objects into JSON using JSON.stringify And sending
I need to send a big number of SIP requests to our server for
I need to send newsletters. I have already a PHP script that sends mass

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.