1

A websocket server receives messages that need to pass to a function as an std::istream. I am using a istringstream as an intermediate step as:

#include <iostream>
#include <sstream>      // std::istringstream, std::stringbuf
#include <string>
...
std::istringstream _iss;
std::istream &_inputbuff = _iss;

My message handler receives the message as a string and sets _iss.str(message).

This approach does not update the stream with the new blocks of data and as a result the next function samples only the last packet. Is there a way to append to the istringstream instead of setting?

Thank you

neiron21
  • 71
  • 5
  • 1
    Per [this answer](https://stackoverflow.com/questions/8786183/how-to-append-content-to-stringstream-type-object) it looks like you'll want to set certain flags in the sstream constructor. See also the [mode](https://en.cppreference.com/w/cpp/io/basic_stringstream/basic_stringstream) parameter. – Nathan Pierson May 10 '21 at 21:53
  • 3
    `std::istringstream` is a read-only stream (hence the `i` in its name, meaning **input**), so it does not allow for appending new characters once the stream has been created. `std::stringstream`, on the other hand, is a read/write stream. You can append characters using `operator<<`, `put()`, etc and then read them back out as needed. – Remy Lebeau May 10 '21 at 22:32
  • Thank you @RemyLebeau, this actually worked! – neiron21 May 11 '21 at 03:05

1 Answers1

0

Better to use std::stringstream and use put(...) function for incoming data (to fill your buffer) and later consume it as you are doing.

stringstream STL reference: https://cplusplus.com/reference/sstream/stringstream/

Shawn
  • 47,241
  • 3
  • 26
  • 60
juanma
  • 1
  • 1