If you close a connection before you've read all the data left in the read
buffer, that data seems to stay around until the next time that socket is used,
and gets incorrectly delivered as data having been read in the new connection.
You can see it happening by trying the WiFiDatastreamUpload example in the Cosm
library <https://github.com/amcewen/Cosm-Arduino> - the first upload will
succeed (or if you've not set the API key or feed id correctly, fail with a
-40x error code) and subsequent attempts will fail with an error of -4 (which
the HttpClient library returns if it doesn't understand the response - because
it's being fed data from the previous request)
Adding a call to flush() during stop() seems to fix the problem.
void WiFiClient::stop() {
if (_sock == 255)
return;
+ flush();
ServerDrv::stopClient(_sock);
unsigned long start = millis();
// wait a second for the connection to close
while (status() != CLOSED && millis() - start < 1000)
delay(1);
_sock = 255;
}
Original issue reported on code.google.com by adrian.m...@gmail.com on 3 Sep 2012 at 6:31
Original issue reported on code.google.com by
adrian.m...@gmail.com
on 3 Sep 2012 at 6:31