【发布时间】:2012-10-15 04:37:09
【问题描述】:
我正在编写 TCP 客户端,它应该能够同时发送和接收数据。
你能告诉我应该如何调用async_send 和async_receive 是单独的线程吗?
也就是说如何调用
m_Socket.async_send(boost::asio::buffer(txBuf.c_str(), txBuf.length()+1),
boost::bind(&TCPClient::sendingHandler, this, boost::asio::placeholders::error));
m_Socket.async_receive(boost::asio::buffer(rxBuf, maxBufLen),
boost::bind(&TCPClient::sendingHandler, this, boost::asio::placeholders::error));
在
boost::thread receivingThread(boost::bind(...));
boost::thread sendingThread(boost::bind(...));
如果我在处理程序中再次调用async_send 或async_receive,它会正常工作吗?我需要一个不定式循环来发送/接收数据。
【问题讨论】:
-
您不需要单独的线程来同时发送/接收。只需在同一个线程中调用
async_send和async_receive,io_service就会为您服务。
标签: c++ boost-asio boost-thread