Question

I would like to know if it is possible to, for instance, take a piece of data in memory, read it into an output stringstream (as binary data) and write this onto a socket for a client application to process.

The problem I run into while attempting this is the following:

Example:

char name[1024] = "Test";
std::ostringstream message (std::stringstream::out | std::stringstream::binary);

len = strlen(name);
message.write(reinterpret_cast<const char*>(&len), sizeof(int));
message.write(test, len*sizeof(char));

I want to write this stringstream to the socket with all of the data in it, but the problem is this: The stringstream write only executes the first time, in this case writing 4 (the length of the string) and none of the subsequent writes. Am I missing something here?

If this is not the best way to do it, what would be the best way to accomplish this? This is partly to reduce file I/O for cached memory snapshots.

Thanks in advance..

Was it helpful?

Solution

Your code (with minor fixes) appears to work for me, so you might check to be sure that you are correctly handling the buffered binary data, i.e. you do not assume that the std::string contains a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top