Reputation: 1745
I'm trying to set up a function to write serialized data to a socket, however I'm at a loss as to what object I'm supposed to be wrapping the boost::buffer around. An example in the official boost documentation uses an std::ostringstream and constructs the buffer from the str() member (of type std::string), but I'm not being able to do the same.
sendData(const std::vector<_t> values){
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << values;
int n = client_sock.write_some(boost::asio::buffer(&oa,sizeof(oa),ec);
}
When I try to use ss.str() instead of oa to construct the buffer, I get:
error: no matching function for call to buffer(std::basic_ostringstream<char>::__string_type*&, long unsigned int&, boost::system::error_code&)’`
int n = client_sock.write_some(boost::asio::buffer(&ss.str(),sizeof(ss.str()),ec);
Upvotes: 1
Views: 2742
Reputation: 393134
You can simply see a number of overloads in the documentation
std::string s = ss.str();
client_sock.write_some(boost::asio::buffer(s),ec);
Alternatively, just use an `
boost:::asio::streambuf sb;
std::ostream os(&sb);
// serialize into `os`
boost::asio::write(client_sock, sb);
Caution You cannot use local variables with async calls because the buffer needs to stay around
That said, since you're using synchronous IO anyways, you can use the streams implemented in Boost Asio:
ip::tcp::iostream stream;
stream.expires_from_now(boost::posix_time::seconds(60));
stream.connect("www.boost.org", "http");
stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
stream << "Host: www.boost.org\r\n";
stream << "Accept: */*\r\n";
stream << "Connection: close\r\n\r\n";
stream.flush();
std::cout << stream.rdbuf();
Upvotes: 3