I have a server class with will start two threads, one is to accept all the incoming connection and put those sockets into a vector. Another one is to scan through this socket vector and find out which one is read to be read/write and then do something.
So the problem is that in the second thread (member function), how do I actually scan those sockets?
people suggest using select() of unix socket but i have decided to use boost everywhere, so I am looking for solution in boost.
I have read some documentation from boost.org and this site. People said that io_service is abstract of select. I don't understand. Do i just bind the function/objects and then use io_service::post() put them into the queue and use io_service::run() to execute?
update, in my understanding, every time you instantiate a new socket object with a constructor including a io_service object, this socket object is associate with the io_service object, since I only have one io_service object, these socket object will be executed when I call io_service.run()
but, the question is, how do i assign handle_function to those socket, I saw people use acceptor.async_accept(socket, boost::bind(handler, &socket)) like this, But I want to use accept rather than async_accept(), but the accept() function cant be used to bind a handler function to this socket
what should i do?
class Server
{
io_service IOService;
std::vector<boost::....::socket> SocketVector;
listen()
{
//blockingly accept all incoming connection and bind socket with handler function;
}
process HandleRequest()
{
//io_service.run()
}
}
int main()
{
Server myServer;
boost::thread thread1(&Server::listen, &myServer);
boost::thread thread2(&Server::HandleRequest, &myServer);
}
Boost.Asio. Finally, you'll end up with a horrible design that doesn't fit the tools you've selected. Instead, it's worth readingAsiomanual and the related resources (blog.think-async.com) thoroughly and trying to "think asynchronously". This way you'll get much better design that would exploit Asio capabilities. - Igor R.io_service::run()(though you can use threads for scalability reasons). With the async (proactor) approach you get notified by means of a completion-handler whenever an i/o-related event occurs, i.e. when a socket gets accepted, when data arrives etc. boost.org/doc/libs/1_53_0/doc/html/boost_asio/overview/core/… - Igor R.