C ++ stl-based sending serialized binary data for network transmission using sockets: Please, neither Boost, nor Protobuf, nor such similar enthusiasts

I need to send some complex objects over the network to a peer. I wrote code to serialize them using ostream and the <operator for each member of the class in objects that need to be serialized. The code I wrote works successfully for serialization and network transmission (htonl (), htons (), etc. It’s used correctly. I checked the above by writing to the stream (local file) in binary format (std :: ios :: bin). The next task that writes this binary data over a network socket is that I am having problems.

I have a Socket class that translates std :: string objects into C-style strings before sending them through a socket as follows:

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

the same Socket class that I use in the receiver uses recv () to put the incoming message in 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 am 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).

So something is wrong with my call to send binary std :: string to the socket. Any help is appreciated. Once again, I do not want to use Boost, protobuf, etc.

PS: , , , , Boost. , non-Boost, non-Protobuf, . , Boost Protobuf , , . .

, .

+5
3

, std::string . goodies = buf;. std::string char * , NUL. NUL, , .

std::vector<char> std::vector<unsigned char>. , , .

std::copy . . , , , <= 0. , , ::recv() .

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;      
  } 
}
+8

, , "send" goodies.size() . : , goodies.size() . , , , goodies const ref, , .

, , , recv . , , . , , , , SCTP, , . , , .

, BTW, , , .

+7

, , , , std::ostringstream, , , . void ostringstream::str(string)? ? , , , . , stringstream, stringstream ( ios_base::in | ios_base::out.

+1

All Articles