Page 1 of 8
WiFiClient example
Posted:
Tue Aug 18, 2015 11:44 am
by lajolo
Hello,
in order to see the WiFiClient example running correctly, I had to change:
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
into this:
// Read all the lines of the reply from server and print them to Serial
while(client.connected()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Could you please tell me if this is correct?
Thanks,
Marcello
Re: WiFiClient example
Posted:
Tue Aug 18, 2015 11:56 am
by kolban
Howdy Marcello,
I think your question hinges on the concept of client.available() vs client.connected().
I think client.available() returns the number of bytes that are available to be read right away. i.e. how many bytes are available to be read without having to wait for new ones to arrive.
The client.connected() returns true if the network connection between the two TCP partners is still good.
By looking at these functions in this light, we see that they perform dramatically different tasks. If your goal is to continue receiving data from the client until it disconnects, then it is likely you will want to use client.connected().
Neil
Re: WiFiClient example
Posted:
Tue Aug 18, 2015 12:08 pm
by lajolo
Hello,
yes, you interpreted correctly my question.
The WiFiClient was not working with client.available() since I was receiving immediately:
closing connection.
I believe that the example should use client.connected() and I would be interested to know if others also had a similar experience with this example.
Thanks,
Marcello
Re: WiFiClient example
Posted:
Tue Aug 18, 2015 1:06 pm
by martinayotte
The WifiClient example is working fine. As Kolban said, connected() only returns the state of the connection while the available() returns the amount of character already received. Make sure also to have a delay() between the request send and the response loop, which is present in the example, otherwise available() will returns 0 since the response has not yet been received.
Maybe you should provide us your sources to figure out what is wrong.