Hi

I am new C++ programming and have only recently started trying to learn network programming using Boost.Asio. I've created a tcp asynchronous server, which works fine, but it is based on a callback technique, and therefore does not keep the connection alive. As soon as the message has been received a message is sent out, and the connection is lost. The code involves 3 classes, a tcp_server, tcp_acceptor and a tcp_service. The acceptor accepts new connections and passes the connection onto the tcp_service, which sends and receives data to/from the client. The server is the main class that starts the acceptor.

Could anyone perhaps help with suggesting how I might modify it to keep the connection alive and thus any further streams of data from the client can be handled?

The asynchronous server code is somewhat lengthy, but I've tried to shorten it as much as I can:
Code:
#include <boost/asio.hpp>

#include <thread>
#include <atomic>
#include <memory>
#include <iostream>

using namespace boost;

class tcp_service {
private:
	std::shared_ptr<asio::ip::tcp::socket> sk;
	std::string response;
	asio::streambuf buf;
	void on_request_received(const boost::system::error_code& ec, std::size_t bytes_transferred) {
		if (ec != 0) {
			std::cout << "Error code = " << ec.value()	<< " : " << ec.message();
			terminate();
			return;
		}
		// Process the request.
		response = process_request(buf);
		// Initiate asynchronous write operation.
		asio::async_write(*sk.get(), asio::buffer(response),
      [this] (const boost::system::error_code& ec, std::size_t bytes_transferred)
            { on_response_sent(ec, bytes_transferred); });
	}

	std::string process_request(asio::streambuf & request) {
		// Do some work.....
		std::this_thread::sleep_for(std::chrono::milliseconds(300));

    boost::asio::streambuf::const_buffers_type bufs = request.data();
    std::string str(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + request.size());
    
		std::string ret { "Response message " + str + "\n"};
		return ret;
	}

	void on_response_sent(const boost::system::error_code& ec, std::size_t bytes_transferred) {
		if (ec != 0) {
			std::cout << "Error code = " << ec.value()	<< " : " << ec.message();
		}
		// terminate();
	}

	void terminate() { delete this; } // Cleanup

public:
	tcp_service(std::shared_ptr<asio::ip::tcp::socket> sock) : sk(sock)	{ }
	void start_handling() {
		asio::async_read_until(*sk.get(), buf, '\n',
                   [this]( const boost::system::error_code& ec, std::size_t bytes_transferred)
                              { on_request_received(ec, bytes_transferred); }
                          );
	}
};

class tcp_acceptor {
public:
	tcp_acceptor(asio::io_service& ios, short port) :
          m_ios(ios),
          acc(m_ios,	asio::ip::tcp::endpoint( asio::ip::address_v4::any(), port)),
          inactive(false)	{ }

	// Start accepting connection requests.
	void start() {
		acc.listen();
		init_accept();
	}

	// Stop accepting connection requests.
	void stop() {
		inactive.store(true);
	}

private:
	void init_accept() {
		std::shared_ptr<asio::ip::tcp::socket> sk(new asio::ip::tcp::socket(m_ios));
		acc.async_accept(*sk.get(),
			[this, sk](	const boost::system::error_code& ec) {	on_accept(ec, sk); });
	}

	void on_accept(const boost::system::error_code& ec, std::shared_ptr<asio::ip::tcp::socket> sk)	{
		if (ec == 0)
			(new tcp_service(sk))->start_handling();
		else
			std::cout << "Error code = "	<< ec.value()	<< " : " << ec.message();

		// Initialise accept
		if (!inactive.load())
			init_accept();
		else
			acc.close(); // Stop accepting connections and free resources.
	}

private:
	asio::io_service& m_ios;
	asio::ip::tcp::acceptor acc;
	std::atomic<bool> inactive;
};

class tcp_server {
public:
	tcp_server() { work.reset(new asio::io_service::work(ios) ); }

	// Start the server.
	void start(short port, short thread_pool_size) {

		assert(thread_pool_size > 0);

		// Create and start tcp_acceptor.
		acc.reset(new tcp_acceptor(ios, port));
		acc->start();

		// Create threads and add to the pool.
		for (short i{0}; i < thread_pool_size; i++) {
			std::unique_ptr<std::thread> th(new std::thread([this]() { ios.run(); }));
			thread_pool.push_back(std::move(th));
		}
	}
	
	void stop() { // Stop server.
		acc->stop();
		ios.stop();

		for (auto& th : thread_pool)
			th->join();
	}

private:
	asio::io_service ios;
	std::unique_ptr<asio::io_service::work> work;
	std::unique_ptr<tcp_acceptor> acc;
	std::vector<std::unique_ptr<std::thread>> thread_pool;
};

int main() {
	short port = 3333;
	try {
		tcp_server srv;

		short thread_pool_size {2};
		srv.start(port, thread_pool_size);

    std::string input_str;
    while(input_str != "q" && input_str != "Q") {
      std::cin >> input_str;
    }
		srv.stop();
	}
	catch (system::system_error &e) {
		std::cout << "Error code = "	<< e.code() << ". Message: " << e.what();
	}

	return 0;
}