How can I listen to two socket simultaneously using boost::asio? I think there shouldn’t be two io_service. Should I resolve two queries for two acceptors to listen to two sokets? Supposed the two sockets are:
127.0.0.1:7001
127.0.0.2:7001
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are correct in using one asio
io_service. (You are of course allowed to use as many as you would like, but it’s more efficient to just use one since it blocks in a thread and you avoid unnecessary context switching. Also, you can callio_service::runfrom multiple threads should you wish to use multiple processors.)Encapsulate the
acceptorand thesocketin a class as is demonstrated in many asio examples, and pass the io_service to the class constructor by reference. You can thenbindthe acceptor to the desired address,listenand callasync_acceptin a class method that you call.