![](/img/trans.png)
[英]calling boost::asio::tcp::socket methods after async_read handler returned an error in server
[英]Async read for tcp server print the data sent by the socket of the client with boost::asio
我要打印客戶端發送的消息。 但是在我的handleRead函數中,當從緩沖區獲取數據並打印時,什么也沒打印。 我不知道該怎么做。
void Network::start()
{
boost::asio::async_write(m_socket, boost::asio::buffer(m_message),
boost::bind(&Network::handleWrite,shared_from_this(),
boost::asio::placeholders::error));
}
void Network::handleWrite(const boost::system::error_code &error)
{
if (!error)
doRead();
else
std::cout << error.message() << std::endl;
}
void Network::handleRead(const boost::system::error_code &error)
{
if (!error) {
std::cout << m_buffer.data()<< std::endl;
doRead();
}
else
close();
}
void Network::doRead()
{
boost::asio::async_read(m_socket,
boost::asio::buffer(m_buffer),
boost::bind(&Network::handleRead,
shared_from_this(),
boost::asio::placeholders::error));
}
m_buffer
的類型是m_buffer
? 如果是容器,則必須提前調整大小。 如果它是asio::streambuf
,則至少需要使用bytes_transferred
handleRead
的bytes_transferred
參數。
回聲示例很多,所以讓我指出幾個:
直到緩沖區的大小已滿時,async_read才會輸出消息!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.