Problem finally found!

My problem was that acceptor_ was called twice and two sessions were created. The first session was used properly while the second session missed the socket setup. class server { public: server(boost::asio::io_service& io_service, const tcp::endpoint& listen_endpoint, mrv::ViewerUI* v) : io_service_(io_service), acceptor_(io_service, listen_endpoint), ui_( v ) { start_accept(); } void start_accept() { tcp_session_ptr new_session( boost::make_shared<tcp_session>( boost::ref( io_service_ ), boost::ref(ui_) ) ); std::cerr << "******** TCP SESSION " << new_session << std::endl; acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, _1)); } void handle_accept(tcp_session_ptr session, const boost::system::error_code& ec) { if (!ec) { session->start(); } start_accept(); // BAD } Changed to: void handle_accept(tcp_session_ptr session, const boost::system::error_code& ec) { if (!ec) { session->start(); } else { start_accept(); } } I still don't know why this is so and the problem is not fully resolved, as only the server change in the timeline works only once, but at least the server now works.

It's fine to call start_accept() inside the handle_accept() function, if you don't do it your server will not be able to accept another client You should check if the ioservice has enough threads to handle more than one client, maybe you have a single thread being blocked by the call to session->start(), if this is the case, your server cannot accept another client until the thread is released (make sure the ioservice has an oioservice::work object attached, btw). __________ Saludos! Juan On Fri, Jan 4, 2013 at 2:30 PM, Gonzalo Garramuno <ggarra13@gmail.com>wrote:
My problem was that acceptor_ was called twice and two sessions were created. The first session was used properly while the second session missed the socket setup.
class server { public: server(boost::asio::io_**service& io_service, const tcp::endpoint& listen_endpoint, mrv::ViewerUI* v) : io_service_(io_service), acceptor_(io_service, listen_endpoint), ui_( v ) { start_accept(); }
void start_accept() { tcp_session_ptr new_session( boost::make_shared<tcp_**session>( boost::ref( io_service_ ), boost::ref(ui_) ) );
std::cerr << "******** TCP SESSION " << new_session << std::endl;
acceptor_.async_accept(new_**session->socket(), boost::bind(&server::handle_**accept, this, new_session, _1)); }
void handle_accept(tcp_session_ptr session, const boost::system::error_code& ec) { if (!ec) { session->start(); } start_accept(); // BAD }
Changed to:
void handle_accept(tcp_session_ptr session, const boost::system::error_code& ec) { if (!ec) { session->start(); } else { start_accept(); } }
I still don't know why this is so and the problem is not fully resolved, as only the server change in the timeline works only once, but at least the server now works. ______________________________**_________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/**mailman/listinfo.cgi/boost-**users<http://lists.boost.org/mailman/listinfo.cgi/boost-users>

Hi Juan, I wasn't asked, but it looks like some kind of MS Visio. Visio is fine for small diagrams, but it's a nightmare for bigger ones because of the over-smart connection arrows... For bigger things I'm using EnterpriseArchitect. Cheers, Ingo "Juan Ramírez" <juanantonio.ram@gmail.com> hat am 6. Januar 2013 um 03:55 geschrieben:
It's fine to call start_accept() inside the handle_accept() function, if you don't do it your server will not be able to accept another client
You should check if the ioservice has enough threads to handle more than one client, maybe you have a single thread being blocked by the call to session->start(), if this is the case, your server cannot accept another client until the thread is released (make sure the ioservice has an oioservice::work object attached, btw).
__________ Saludos! Juan
On Fri, Jan 4, 2013 at 2:30 PM, Gonzalo Garramuno <ggarra13@gmail.com <mailto:ggarra13@gmail.com> > wrote:
My problem was that acceptor_ was called twice and two sessions were created. The first session was used properly while the second session missed the socket setup.
class server { public: server(boost::asio::io_ service& io_service, const tcp::endpoint& listen_endpoint, mrv::ViewerUI* v) : io_service_(io_service), acceptor_(io_service, listen_endpoint), ui_( v ) { start_accept(); }
void start_accept() { tcp_session_ptr new_session( boost::make_shared<tcp_ session>( boost::ref( io_service_ ), boost::ref(ui_) ) );
std::cerr << "******** TCP SESSION " << new_session << std::endl;
acceptor_.async_accept(new_ session->socket(), boost::bind(&server::handle_ accept, this, new_session, _1)); }
void handle_accept(tcp_session_ptr session, const boost::system::error_code& ec) { if (!ec) { session->start(); } start_accept(); // BAD }
Changed to:
void handle_accept(tcp_session_ptr session, const boost::system::error_code& ec) { if (!ec) { session->start(); } else { start_accept(); } }
I still don't know why this is so and the problem is not fully resolved, as only the server change in the timeline works only once, but at least the server now works. ______________________________ _________________ Boost-users mailing list Boost-users@lists.boost.org <mailto:Boost-users@lists.boost.org> http://lists.boost.org/mailman/listinfo.cgi/boost-users <http://lists.boost.org/mailman/listinfo.cgi/boost-users>
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 05/01/13 23:55, Juan Ramírez wrote:
It's fine to call start_accept() inside the handle_accept() function, if you don't do it your server will not be able to accept another client
You should check if the ioservice has enough threads to handle more than one client, maybe you have a single thread being blocked by the call to session->start(), if this is the case, your server cannot accept another client until the thread is released (make sure the ioservice has an oioservice::work object attached, btw).
__________ Saludos! Juan
Thanks, Juan for the help. I realized that the problem was my code was storing a pointer to the session globally and when two sessions were started, there would be an overlap. My quick fix of start_accept was indeed making the server not take another client. I have a better fix now, but I will need to work on the code much more to make it solid.
participants (3)
-
Gonzalo Garramuno
-
ingo@liquidcooling.de
-
Juan Ramírez