hello, I start using asio in my project and I don't know how to reuse a socket... some code: void server(asio::io_service& io_service, short port) { tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port)); for (;;) { tcp::socket *sock=new tcp::socket(io_service); a.accept(sock); session(sock); } } void session(tcp::socket *sock) { sock->read(...); .... sock->send("reply"); } when I send the first request everything is ok but when I try for a second one I never get a reply or an error from the server... The easy way would be to have a thread/connection model so I could block each thread waiting to a socket but I want to use a thread-pool for my server so I want to know if there is any callback parameter I could pass to my socket and when there is data availiable in the socket the asio would fire the callback function . thanks in advance, george
Hi George, george wrote:
I start using asio in my project and I don't know how to reuse a socket... some code: [code snipped] when I send the first request everything is ok but when I try for a second one I never get a reply or an error from the server...
It's hard to say why that would be the case without seeing the real program, but judging by your next paragraph it's more to do with the design approach you've taken.
The easy way would be to have a thread/connection model so I could block each thread waiting to a socket but I want to use a thread-pool for my server so I want to know if there is any callback parameter I could pass to my socket and when there is data availiable in the socket the asio would fire the callback function .
Yes you could use a thread-per-connection design, but I wouldn't recommend it :) Instead, I suggest you use the async_* functions to have the reads and writes performed asynchronously. These functions take a callback which is called after the asynchronous operation completes. If you work through the daytime tutorial programs you will see how this approach compares to the "iterative" design you have now. Cheers, Chris
Hello Chris, thanks for your reply and for this wonderfull lib. I am wondering if I will be able sometime after years to be in this level as a C++ programmer as you (and the rest of Boosters)....close to you;-) I know I can do this with async_* functions but becouse I am a beginner with the lib I choose to use first the sync_* functions for my thread-pool.... At the moment I use the async_* functions and my threads are call the io_service::run() method. My application is thread-safe, for asio is there any detail I should take care when I call io_service:run() from many threads? thanks in advance, george
Hi George, george wrote:
At the moment I use the async_* functions and my threads are call the io_service::run() method. My application is thread-safe, for asio is there any detail I should take care when I call io_service:run() from many threads?
The io_service::run() function is designed to support being called from multiple threads, so you don't need to do anything special in that part of your application. However, if you call io_service::run() from multiple threads then it's possible for several of your callback handlers to be executed concurrently. If you have handlers that you do not want running concurrently (e.g. all handlers associated with a single connection object) you may want to look at using a strand: http://asio.sourceforge.net/boost_asio_0_3_7/libs/asio/doc/tutorial/tuttimer... Cheers, Chris
participants (2)
-
Christopher Kohlhoff
-
george