L
L
lightalex2017-05-13 23:59:58
C++ / C#
lightalex, 2017-05-13 23:59:58

What is the correct way to read with boost::asio::read up to a certain character?

Hello!
There is the following C++ code for receiving responses to POST requests:

boost::asio::write(*socket, request);
std::vector<char> response(10 * 1024);
boost::system::error_code ec;
auto bytes_received = boost::asio::read(*socket, boost::asio::buffer(response), boost::asio::transfer_all(), ec);
std::cout << response.data() << std::endl;

The problem is that I'm trying to keep the connection (connection: keep-alive) and due to this the server does not report the end, and the response is given in size larger than the expected message.
So it takes the response before the timeout, which is quite a long time ...
I'm trying make it accept the answer up to a certain character and not wait for the end
Alas, nothing has happened yet :(
Is it even possible?
If so, please help! I would be very grateful!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
15432, 2017-05-14
@lightalex

I would take the data into the buffer and parse it already.
1) as soon as data appears in the socket -> read into the buffer
2) after each read, check if the full HTTP header has formed in the buffer
3) as soon as the header is typed, parse and bite it
4) similarly, we wait in parts, collect and parse JSON
5) we repeat everything from the beginning - we are waiting for a new HTTP header and so on
my proxy is here, where the devil will break his leg
(I doubt that it will help much xD)

A
Andrey_1984, 2019-08-14
@Andrey_1984

Sorry, unfortunately, I could not understand the advice from 15432:

я б принимал данные в буфер и парсил уже его. 
1) как только в сокете появляются данные -> читаем в буфер
2) после каждого чтения проверяем, не сформировался ли в буфере полный HTTP заголовок

Please tell me, if not difficult, but how to do it?
1) That is, here is point 1, as soon as data appears in the socket, we read them into the buffer. But the read function itself will read them into the streambuf provided to it, for example, automatically. We don't read by hand, do we?
2) And unfortunately I could not understand the second point at all. For example, as the TS wrote, a keepalive connection is maintained with the server, that is, the server has transmitted data to you, but the connection does not close, which means the read function is blocked. That is, read is still waiting. How can you read something from the buffer if the read function is still executing??

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question