Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::asio::io_service to retrieve data in main
    primarykey
    data
    text
    <p>Come across the following codes (from user368831) which is what I am looking for. I have modified a little to make it a threaded TCP session that listen and read for connection and data while the main loop can do other tasks.</p> <pre><code>class CSession { public: CSession(boost::asio::io_service&amp; io_service) : m_Socket(io_service) {} tcp::socket&amp; socket() return m_Socket; void start() { boost::asio::async_read_until(m_Socket, m_Buffer, " ", boost::bind(&amp;CSession::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void handle_read(const boost::system::error_code&amp; error, size_t bytes_transferred) { if (!error) { ostringstream ss; ss &lt;&lt; &amp;m_Buffer; m_RecvMsg = ss.str(); std::cout &lt;&lt; "handle_read():" &lt;&lt; m_RecvMsg &lt;&lt; std::endl; } else delete this; } private: boost::asio::streambuf m_Buffer; tcp::socket m_Socket; string m_RecvMsg; }; class CTcpServer { public: CTcpServer(short port) : m_Acceptor(m_IOService, tcp::endpoint(tcp::v4(), port)), m_Thread(boost::bind(&amp;boost::asio::io_service::run, &amp;m_IOService)) { CSession* new_session = new CSession(m_IOService); m_Acceptor.async_accept(new_session-&gt;socket(), boost::bind(&amp;CTcpServer::handle_accept, this, new_session, boost::asio::placeholders::error)); }; void handle_accept(CSession* new_session, const boost::system::error_code&amp; error) { if (!error) { new_session-&gt;start(); new_session = new CSession(m_IOService); m_Acceptor.async_accept(new_session-&gt;socket(), boost::bind(&amp;CTcpServer::handle_accept, this, new_session, boost::asio::placeholders::error)); } else delete new_session; } private: boost::asio::io_service m_IOService; tcp::acceptor m_Acceptor; boost::thread m_Thread; }; void main() { : CTcpServer *server = new CTcpServer(6002); // tcp port 6002 /* How to get the incoming data sent from the client here?? */ // string message; // while(1) // { // if ( server-&gt;incomingData(message) ) // { // std::cout &lt;&lt; "Data recv: " &lt;&lt; message.data() &lt;&lt; std::endl; // } // : // : // other tasks // : // } } </code></pre> <p>However, how do I code incomingData() in the main loop such that it will monitor the data from the client and return true whenever handle_read() is called?</p> <p>Can use Boost::signals library in this case? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload