开发者

boost::asio::streambuf empty?

I am using boost::asio::streambuf to write a ostream to my boost socket.

Name name;
name.set_nam开发者_运维百科e("platzhirsch");

boost::asio::streambuf b;
std::ostream os(&b);

ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&os);
CodedOutputStream *coded_output = new CodedOutputStream(raw_output);

coded_output->WriteVarint32(name.ByteSize());
name.SerializeToCodedStream(coded_output);

socket.send(b.data());

However, size_t returned by send is 0. I am suspicious that no data is sent at all. Also because the client socket throws horrible exceptions. I am asking, if there is something strange about my code.

In other words, can I test if streambuf is empty or if the data written to it is really there.


Not sure about your code. This works for me:

    boost::asio::streambuf request;
    std::ostream request_stream(&request);
    request_stream << "GET " << queryArgs << " HTTP/1.0\r\n";
    request_stream << "Host: " << serverIp  /* "192.168.0.70" */ << "\r\n";
    request_stream << "Accept: */*\r\n";
    request_stream << "Connection: close\r\n\r\n";

    // Send the request.
    boost::asio::write(socket, request);


It was no problem of boost::asio, it was a problem of Google protobuf.

In order to flush the CodedOutputStream, they have to be deleted:

ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&os);
CodedOutputStream *coded_output = new CodedOutputStream(raw_output);

coded_output->WriteVarint32(name.ByteSize());
name.SerializeToCodedStream(coded_output);

delete coded_output;
delete raw_output;

socket.send(b.data());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜