我正在使用boost :: asio :: streambuf將一個ostream寫入我的boost套接字。boost :: asio :: streambuf爲空?
Name name;
name.set_name("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());
但是,由send返回的size_t是0.我懷疑根本沒有數據發送。還因爲客戶端套接字引發了可怕的異常。我問,如果我的代碼有些奇怪的話。
換句話說,我可以測試streambuf是否爲空或寫入的數據是否真的存在。
你不必動態分配數據流,但可以使用的功能塊控制堆的壽命對象,而不是。請參閱http://stackoverflow.com/a/20397980/891439獲取完整答案。我只是添加了這個評論的完整性,因爲我不得不穀歌尋找解決方案。 – 2013-12-05 10:53:11