[asio] Clients (try to) connect only once
Hi I am writing on a an app, that has clients, which connect, and get events/updates send. Now, it works almost, but still, I got one problem I couldn't solve sofar: My Client only connects or tries to connect once. If the server isn't running, I have to restart the app, to be able to try again. I do get a connection event at the server, but the client seems not to respond to the send data afterwards. I am not sure, if it might be a threading issue, but i think that calling join() should do the job. My Code: void ZMSRClientFrame::OnConnect(wxCommandEvent& event) { // the behavoir happens also with out those 2 ifs (just tried to check that) if(inputhandler) { inputhandler->close(); } if(t) { t->join(); t.reset(); } try{ boost::asio::ip::tcp::resolver resolver(ios); boost::asio::ip::tcp::resolver::query query("127.0.0.1", "3210"); boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query); //InputHandler in(ios,iterator); inputhandler = boost::shared_ptr<InputHandler>(new InputHandler(this,ios,iterator)); //ios.run(); //starting ioservice thread t = boost::shared_ptrboost::thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &ios))); } catch(std::exception& e) { wxMessageBox(e.what()); //inputhandler.reset(); } catch(...) {wxMessageBox("could not connect"); //inputhandler.reset(); } } Inputhandler calls async_connect which returns to one of its members. Which works through the first run. How to solve this? regards Jens Weller -- Jetzt 1 Monat kostenlos! GMX FreeDSL - Telefonanschluss + DSL für nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
Inputhandler calls async_connect which returns to one of its members.
Which
works through the first run.
How to solve this?
Probably, your io_service object has no more pending work, so its run() loop exits? Look at the "work" class in asio reference.
Hm, thas very much possible. I thought I could start the io_service object again, if there is new need for it. Maybe I should make a Session class, which holds its own io object and inputhandler, so I only create a session in my client, and the rest is then tied to the Session instance. Would that work? I've just seen the work object too, this solves the problem too. At least at a first glance, I did not have the time for extensive testing yet. thx. Jens Weller -- Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger01
Hm, thas very much possible. I thought I could start the io_service object again, if there is new need for it.
You can restart io_service::run after it returns, but you should call io_service::reset() first.
Maybe I should make a Session class, which holds its own io object and inputhandler, so I only create a session in my client, and the rest is then tied to the Session instance. Would that work?
The decision who should own io_service(s) depends on your application design. You've got examples of asio-based design here: http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/examples.html
participants (2)
-
Igor R
-
Jens Weller