我正在嘗試創建一個TCP服務器,其中Start()方法阻塞,直到接受連接,然後開始一系列異步讀取。我有以下的代碼,當我連接使用telnet我得到這樣的輸出:Boost async_read
等待
連接已接受
一個新的連接終止叫做拋出exceptionAbort陷阱:6
這裏是代碼:
void SocketReadThread::Start()
{
bzero(m_headerBuffer, HEADER_LEN);
m_running = true;
asio::io_service ios;
asio::ip::tcp::acceptor acp (ios,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), GUI_PORT));
asio::ip::tcp::socket sock(ios);
std::cout << "Waiting for a new connection" << std::endl;
acp.accept(sock);
std::cout << "Connection accepted" << std::endl;
asio::async_read(sock, asio::buffer(m_headerBuffer, HEADER_LEN),
boost::bind(&SocketReadThread::handleReadHeader, shared_from_this(),
asio::placeholders::error));
ios.run();
}
void SocketReadThread::handleReadHeader(const system::error_code& error)
{
std::cout << "Read two bytes!" << std::endl;
}
原來,我以這種方式調用shared_from_this()也是不正確的,但您也是正確的。 – grivescorbett
爲什麼?讀處理程序是靜態的嗎? –