boostorg / boost

Super-project for modularized Boost
https://github.com/boostorg/wiki/wiki/Getting-Started%3A-Overview
Boost Software License 1.0
7.02k stars 1.73k forks source link

Slow reading from socket with boost::asio::read #831

Closed MortezaBashsiz closed 8 months ago

MortezaBashsiz commented 9 months ago

Hi

I want to write a request on socket and read the response from it The part writing to the socket is OK, but the part read is too slow.

boost::system::error_code ec;
boost::asio::write(socket, boost::asio::buffer(message), ec);
if (ec) {
    std::cerr << ec.what();
}
boost::asio::streambuf responseBuffer;
int bytesTransferred = boost::asio::read(socket, responseBuffer, boost::asio::transfer_all(), ec);
if (ec && ec != boost::asio::error::eof) {
    std::cerr << ec.what();
}
std::cout << "DEBUG : " << bytesTransferred << std::endl;
unsigned char tempData[bytesTransferred];
std::memcpy(tempData, boost::asio::buffer_cast<const void*>(responseBuffer.data()), bytesTransferred);

NOTE: Even with using treansfer_atleast(1) or transfer_exactly(1) in a loop, the result is the same. NOTE: I do not know exact size in read and it could be different