1

I am facing problem with ESP32. I use basic example for WiFiClient and making request to my server. The problem is that the response should be 15000 bytes long, but very often I got only 2750 or less.

char buffer[15000];
while (client.available()) {
  char c = client.read();
  buffer[i++] = c;
}
Serial.println(i);

1 Answer 1

1

You are assuming that all 15000 bytes are available all the time.

They won't be.

Instead of "while available read something" you need to "While not enough has been read, then if available read something".

When reading from (assuming) HTTP you first need to read and interpret the headers (the lines before a single blank line). One of these will be the "Content-Length:" header, which will tell you exactly how many bytes are in the body of the response. Then you use that to read the right number of bytes from the body.

So the sequence is:

  1. Read line by line for the headers interpreting the ones you need to, until:
  2. You receive a single blank line
  3. Read the data byte-wise until you have read the number of bytes indicated in the Content-Length header.

If you aren't reading from an HTTP server and you are certain that the data is exactly 15000 bytes in length then you need to skip to point 3 above. Never assume that all the data is there. If there is data then read it, and repeat until you have totalled 15000 bytes.

1
  • Thanks, the problem was probably caused by client.available(), while reading response, it found a gap when client has no data available and so ends the reading. When I havent got enought data, I wait (with timeout) to get some data available.
    – A. L. K.
    Sep 20, 2019 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.