How to keep a socket connection alive using boost::asio?

After you open the acceptor you bind it to an endpoint, this endpoint is what you're accepting on and it (or rather the socket associated with it) gets re-used. The socket reference you pass into async_accept is a new socket that will hold the next incoming connection.

After you open the acceptor you bind it to an endpoint, this endpoint is what you're accepting on and it (or rather the socket associated with it) gets re-used. The socket reference you pass into async_accept is a new socket that will hold the next incoming connection. E.g.In the HTTP Server Boost asio example: server::server(const std::string& address, const std::string& port, const std::string& doc_root) : io_service_(), acceptor_(io_service_), connection_manager_(), new_connection_(new connection(io_service_, connection_manager_, request_handler_)), request_handler_(doc_root) { // Open the acceptor with the option to reuse the address (i.e.

SO_REUSEADDR). Boost::asio::ip::tcp::resolver resolver(io_service_); boost::asio::ip::tcp::resolver::query query(address, port); boost::asio::ip::tcp::endpoint endpoint = *resolver. Resolve(query); acceptor_.

Open(endpoint.protocol()); acceptor_. Set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor_. Bind(endpoint); acceptor_.listen(); acceptor_.

Async_accept(new_connection_->socket(), boost::bind(&server::handle_accept, this, boost::asio::placeholders::error)); } The new_connection object created for every async_accept has a new socket to receive the incoming connection. The acceptor keeps a socket internally for accepting on that is associated with the endpoint you pass it. If you look at handle_accept: void server::handle_accept(const boost::system::error_code& e) { if (!e) { connection_manager_.

Start(new_connection_); new_connection_. Reset(new connection(io_service_, connection_manager_, request_handler_)); acceptor_. Async_accept(new_connection_->socket(), boost::bind(&server::handle_accept, this, boost::asio::placeholders::error)); } } You can see that a new connection object is constructed (and in it a new socket) to hold the next incoming connection.

New_connection_->socket() returns a reference to that socket. The acceptor is still accepting on the same socket or port that was initially passed to it.

If I'm understanding your question correctly it seems the issue you are having is passing the accepted socket connection to a handling method to process the transfers so that the acceptor can be freed up to get new connections. After you accept a connection you (obviously) need to handle the transfers that come across it. So make an acceptor method that accepts a connection, after a connection is made, create a new handling class and pass it a reference to the io_service and the socket the connection came in on, allow that class to process the transfers for that connection and have the acceptor method wait for more connections (and rinse and repeat).

The handling method would be specific to your application.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions