Note that there are some explanatory texts on larger screens.

plurals
  1. POboost asio timer do not work with blocking read call
    text
    copied!<p>I have following server code. which waits for client to connect and once the client connects it will start thread which receives data from client connected and main thread will wait for another client to connect. This code works fine.</p> <p>Now I have to specify that server will wait for some time say 10 sec to receive data from connected client. or otherwise server will close the communication if no data received within specified time. I have implemented timer for the same but somehow it doesnt work and <code>timer_callback</code> is not called after time elapsed.</p> <pre><code>#include &lt;ctime&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;boost/asio.hpp&gt; #include &lt;sys/socket.h&gt; #include &lt;unistd.h&gt; #include &lt;string&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/thread.hpp&gt; #include &lt;boost/date_time.hpp&gt; using namespace std; using boost::asio::ip::tcp; void run(boost::shared_ptr&lt;tcp::socket&gt; my_socket) { while (1) { char buf[128]; boost::system::error_code error; size_t len = my_socket-&gt;read_some(boost::asio::buffer(buf, 128), error); std::cout &lt;&lt; "len : " &lt;&lt; len &lt;&lt; std::endl; if (error == boost::asio::error::eof) { cout &lt;&lt; "\t(boost::asio::error::eof)" &lt;&lt; endl; if (my_socket-&gt;is_open()) { boost::system::error_code ec; cout &lt;&lt; "\tSocket closing" &lt;&lt; endl; my_socket-&gt;shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); cout &lt;&lt; "\tShutdown " &lt;&lt; ec.message() &lt;&lt; endl; //cout &lt;&lt; "normal close : " &lt;&lt; ::close(my_socket-&gt;native_handle()) &lt;&lt; endl; my_socket-&gt;close(ec); cout &lt;&lt; "\tSocket closed" &lt;&lt; endl; } break; // Connection closed cleanly by peer. } else if (error) { std::cout &lt;&lt; "Exception : " &lt;&lt; error.message() &lt;&lt; std::endl; break; } else { for (unsigned int i = 0; i &lt; len; i++) printf("%02x ", buf[i] &amp; 0xFF); printf("\n"); } } } void timer_callback(boost::shared_ptr&lt;tcp::socket&gt; my_socket, const boost::system::error_code&amp; error) { std::cout &lt;&lt; "timer expired.... " &lt;&lt; std::endl; if (error != boost::asio::error::operation_aborted) { // do something std::string str_error = "timer expired " + error.message(); my_socket-&gt;shutdown(boost::asio::ip::tcp::socket::shutdown_both); my_socket-&gt;close(); } else { std::cout &lt;&lt; "timer cancelled ..." &lt;&lt; error.message() &lt;&lt; std::endl; } } int main() { const int S = 1000; vector&lt;boost::shared_ptr&lt;boost::thread&gt; &gt; arr_thr(S); boost::asio::io_service io_service; boost::asio::deadline_timer timer(io_service); try { for (uint32_t i = 0;; i++) { tcp::endpoint endpoint(tcp::v6(), 10001); boost::shared_ptr&lt;tcp::socket&gt; my_socket(new tcp::socket(io_service)); tcp::endpoint end_type; tcp::acceptor acceptor(io_service, endpoint); std::cout &lt;&lt; "before accept" &lt;&lt; endl; acceptor.accept(*my_socket, end_type); usleep(10000); std::cout &lt;&lt; "connected... hdl : " &lt;&lt; my_socket-&gt;native_handle() &lt;&lt; std::endl; boost::asio::ip::address addr = end_type.address(); std::string sClientIp = addr.to_string(); std::cout &lt;&lt; "\tclient IP : " &lt;&lt; sClientIp &lt;&lt; std::endl; timer.expires_from_now(boost::posix_time::seconds(10)); timer.async_wait( boost::bind(&amp;timer_callback, my_socket, boost::asio::placeholders::error)); arr_thr[i] = boost::shared_ptr&lt;boost::thread&gt;(new boost::thread(&amp;run, my_socket)); } } catch (std::exception&amp; e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; } return 0; } </code></pre>
 

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