4
現在我通過以下方式接收文本:使用boost asio接收文本的最有效方法?
boost::asio::streambuf buffer;
std::string text;
while(true) {
try
{
boost::asio::read_until(*m_pSocket, buffer, "END");
text = boost::asio::buffer_cast<const char*>(buffer.data());
buffer.consume(text.size());
boost::asio::write(*m_pSocket, boost::asio::buffer(text, text.size()));
std::cout << text<< std::endl;
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
break;
}
}
收到序列「END」當我剛收到的回波文本到客戶端。我的問題:
在我看來非常inefficent到流緩衝轉換爲字符串,然後消耗從它的文字標誌。以良好,乾淨和高效的方式處理收到的數據的正確方式是什麼?
如果你需要的是發送'streambuf',您可以直接做到這一點,沒有用'string'。 – 2013-02-14 15:15:23
不是。在目前的情況下,這已經足夠了,但是我的應用程序在以後會變得更復雜 – Anonymous 2013-02-14 15:24:03
'buffer_cast''會返回'const char *'。從這一點你可以用這個常量緩衝區來做任何你想要的。 –
2013-02-14 15:29:31