Async tcp socket client: send multiple requests with one connection I'm new to socket programming and c++. I have a the following method that sends an array to server and receives sum of the array, I tried to make it async.
std::future&lt;void&gt; AsyncClient::ReqSum(const std::vector&lt;double&gt;&amp; numbers)
{
    return std::async(
        std::launch::async,
        [&amp;, numbers]()
        {
            // Setup connection. Can move to seperate method?
            boost::asio::io_service _ioservice;
            tcp::socket _socket(_ioservice);
            _socket.connect(m_endpoint);

            std::string requestJson = Serializer::ArraySumRequest(numbers);
            boost::system::error_code err;
            
            boost::asio::write(_socket, boost::asio::buffer(requestJson), err);
            if (err)
            {
                std::stringstream ss;
                ss &lt;&lt; &quot;Couldn't write to socket! error code: &quot; &lt;&lt; err;
                throw ss.str();
            }

            // getting response from server
            boost::asio::streambuf receiveBuffer;
            boost::asio::read(_socket, receiveBuffer, boost::asio::transfer_all(), err);

            if (err &amp;&amp; err != boost::asio::error::eof)
            {
                std::stringstream ss;
                ss &lt;&lt; &quot;Receiving from the server failed! error code: &quot; &lt;&lt; err.message();
                throw ss.str();
            }

            const char* data = boost::asio::buffer_cast&lt;const char*&gt;(receiveBuffer.data());

            rapidjson::Document doc;
            doc.Parse&lt;rapidjson::kParseDefaultFlags&gt;(data);

            if (!doc.HasMember(&quot;result&quot;))
                throw &quot;No result found!&quot;;

            double result = doc[&quot;result&quot;].GetDouble();

            std::cout &lt;&lt; &quot;Sum of the array: &quot; &lt;&lt; result &lt;&lt; std::endl;

            _socket.shutdown(boost::asio::socket_base::shutdown_both);
            _socket.close();
        });
}

Actually I want to know is it possible to use one connection to for multiple requests(i.e move that socket and connection to another method)?
And if there are any fixes to my code please let me know.
I want to know is it possible to use one connection to for multiple requests(i.e move that socket and connection to another method)?

In principle, yes. The easiest way would be to not create a socket in this method, but leave it up to the caller to create a socket, and pass a reference to it to AsyncClient::ReqSum(), so that the socket can be reused for multiple requests.
However, as you mentioned this doesn't work, and that is because you rely on the server to close the connection right after sending the response to a single request:
boost::asio::streambuf receiveBuffer;
boost::asio::read(_socket, receiveBuffer, boost::asio::transfer_all(), err);

The read() command here doesn't know how large the response is, but you told it to transfer all data sent by the server. The only way to signal the end of all data is for the server to close the connection. If the server would support multiple requests on a connection, then you need some way to deliniate responses. And in that case, you will have to use a function like boost::asio::read_until() to read data up to a certain point.
Don't throw strings
First of all, constructing a string using std::stringstream is quite expensive. But the main issue is that you are throwing a plain std::string. The problem is that any error handling code now has no idea what kind of error happened, unless it parses that string. That will also be inefficient.
You should instead throw a type that inherits from std::exception. In this case, either throw a std::runtime_error, or if you wish some custom type that inherits from that.
Avoid using std::endl
Prefer using &quot;\n&quot; instead of std::endl; the latter is equivalent to the former, but also forces a flush of the output stream, which is usually not necessary and might be bad for performance.