I'd like to send multiple strings through TCP without combining them into one big string first, and as I understand ASIO's scatter-gather I/O interface can do this. However, I must be doing something wrong because my implementation keeps running into memory errors. The snippet below (compilable and runnable) returns a garbled string when I telnet localhost 11211
:
#include <vector>
#include <string>
#include <boost/asio.hpp>
using namespace std;
using namespace boost::asio;
using namespace boost::asio::ip;
int main() {
io_service service;
tcp::acceptor acceptor(service, tcp::endpoint(tcp::v4(), 11211));
tcp::socket sock(service);
acceptor.accept(sock);
if (!acceptor.is_open()) return 1;
string s = "this is a really long string";
vector<const_buffer> vec;
vec.push_back(buffer(s));
write(sock, buffer(vec));
}
However, it works fine when I do write(sock, buffer("this is a really long string"))
. What am I missing here?
The last line should be:
write(sock, vec);
Otherwise it's not "scatter-gather", because buffer
free function always returns mutable_buffers_1
, i.e. a single buffer. In your case the following buffer
overload gets called:
template <typename PodType, typename Allocator>
inline mutable_buffers_1 buffer(std::vector<PodType, Allocator>& data)
{
return mutable_buffers_1(mutable_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType)));
}
Since your vector is not a "vector of POD", it's treated incorrectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With