I'm using the code given in.
The server accepts 1 connection at a time. This means there is no new connection until the current one is closed.
How to accept the above code at unlimited access at the same time?
#include & lt; Cstdlib & gt; # Include & lt; Iostream & gt; #include & lt; Memory & gt; # Include & lt; Utility & gt; # Include & lt; Boost / asio.hpp & gt; Boost :: asio :: ip :: tcp is using; Class Season: Public Study :: Enabled / Teacher ____________ the_source_site_stom_this & lt; Session & gt; {Public: Session (TCP :: socket socket): socket_ (std :: move socket) {} zero start () {do_read (); } Private: Make zero () (auto_frame_ ()); Socket_.async_read_some (boost :: asio :: buffer (data_, max_length), [it, itself] (boost :: system :: error_code ec, std :: size_t length) {if (! Ec) {boost :: this_thread: : Sleep (boost: posix_time :: milliseconds (10000); some time do_write (length);}}); } Zero do_write (std :: size_t length) {Auto self (shared_from_this ()); Boost :: asio :: async_write (socket_, boost :: asio :: buffer (data_, length), [this, self] (boost :: system :: errorGod ec, std :: size_t / * length * /) {if (! ECC) {do_read ();}}}; } TCP :: socket socket_; Enum {max_length = 1024}; Char data_ [max_length]; }; Class server {public: server (boost :: aso :: io_service and io_service, mini port): acceptable_ (io_service, TCP :: endpoint (TCP :: V4 (), port), socket_ (io_service) { Do_accept (); } Private: zero-do_accept () {acceptor_.async_accept (socket_, [this] (boost :: system :: error_code ec) {if (! Ec) {std :: make_shared (std :: Move (socket_)) - & gt; Start ();} do_accept ();}); } TCP :: acceptor acceptor_; Tcp :: socket socket_; }; Int main (int argc, char * argv []) {try {if (argc! = 2) {std :: cerr & lt; & Lt; "Use: async_tcp_echo_server & lt; port & gt; \ n"; Return 1; } Boost :: asio :: io_service io_service; Server s (io_service, std :: atoi (argv [1])); Io_service.run (); } Hold (std :: exception & e) {std :: cerr & lt; & Lt; "Exception:" & lt; & Lt; E.what () & lt; & Lt; "\ N"; } Return 0; }
As you see, the program waits for sleep and does not take second connection between this.
You are waiting for a synchronous inside the handler that works on your io_service only run on the thread is. It waits for any new requests, while accessing handlers.
-
Use
deadline_time
withwait_async
, orzero-world () {Auto_images (shared_freem_yes ()); Socket_.async_read_some (boost :: asio :: buffer (data_, max_length), [it, itself] (boost :: system :: error_code ec, std :: size_t length) {if (! Ec) {timer_.expires_from_now (boost :: posix_time :: seconds (1); timer_assic_wright ([it, self, length] (boost :: system :: errorGode EC) {if (! Ec) do_write (length);});}}); }
Where
timer _
field is aboost :: asio :: deadline_timer
member'ssession
Promotion: thread_groups TG; For (Int i = 0; I
Comments
Post a Comment