Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost asio : how to keep a client connection alive?
    primarykey
    data
    text
    <p>I try to create a client who keep the connection alive on the server.</p> <p>However, when i receive data once, the connection was closed. I don't understand why.</p> <p>I think that i should make a loop, but we told me that wasn't a good idea.</p> <pre><code>class client { public: client(boost::asio::io_service&amp; io_service, const std::string&amp; host, const std::string&amp; service) : connection_(io_service) { // Resolve the host name into an IP address. boost::asio::ip::tcp::resolver resolver(io_service); boost::asio::ip::tcp::resolver::query query(host, service); boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator; // Start an asynchronous connect operation. connection_.socket().async_connect(endpoint, boost::bind(&amp;client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator)); } /// Handle completion of a connect operation. void handle_connect(const boost::system::error_code&amp; e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { if (!e) { // Successfully established connection. Start operation to read the list // of stocks. The connection::async_read() function will automatically // decode the data that is read from the underlying socket. connection_.async_read(stocks_, boost::bind(&amp;client::handle_read, this, boost::asio::placeholders::error)); } else { std::cerr &lt;&lt; e.message() &lt;&lt; std::endl; } } /// Handle completion of a read operation. void handle_read(const boost::system::error_code&amp; e) { if (!e) { // Print out the data that was received. for (std::size_t i = 0; i &lt; stocks_.size(); ++i) { std::cout &lt;&lt; "Paquet numero " &lt;&lt; i &lt;&lt; "\n"; std::cout &lt;&lt; " age: " &lt;&lt; stocks_[i].age &lt;&lt; "\n"; std::cout &lt;&lt; " name: " &lt;&lt; stocks_[i].nom &lt;&lt; "\n"; } // Maybe Should i put something here ? } else { // An error occurred. std::cerr &lt;&lt; "Error : " &lt;&lt; e.message() &lt;&lt; std::endl; connection_.socket().close(); } // or maybe here ? } private: /// The connection to the server. connection connection_; std::vector&lt;stock&gt; stocks_; }; </code></pre> <p>the main looks like this :</p> <pre><code>int main(int argc, char* argv[]) { try { // Check command line arguments. if (argc != 3) { std::cerr &lt;&lt; "Usage: client &lt;host&gt; &lt;port&gt;" &lt;&lt; std::endl; return 1; } boost::asio::io_service io_service; client client(io_service, argv[1], argv[2]); io_service.run(); } catch (std::exception&amp; e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; } return 0; } </code></pre>
    singulars
    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.
    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