Go Down

Topic: Infinite loop at while(!client.available()) (Read 3155 times) previous topic - next topic

woodwax

Aug 21, 2015, 10:30 pm Last Edit: Aug 21, 2015, 11:51 pm by robtillaart
Hello

I running webserver on my ESP8266. Also its gets NTP time data from server in every 100 seconds, and have a Ticker with 10second cycle. Its going very well but sometimes at line:

Code: [Select]
while(!client.available())   
{
  Serial.print(".");   
  delay(1);   
}// Wait until the client sends some data


It falls and only write out "." on serial port, infinite loop. Does anyone have ideas?

I attach part of my code.

robtillaart

change the while in an if  and only act when there is data

if (client.available() > 0)   
{
   // process received data
}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

nickgammon

Quote
Also its gets NTP time data from server in every 100 seconds ...
The internal clock isn't that inaccurate. You'll be IP blocked by the NTP server before you know it.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

SurferTim

#3
Aug 22, 2015, 12:30 am Last Edit: Aug 22, 2015, 12:37 am by SurferTim
Quote
It falls and only write out "." on serial port, infinite loop. Does anyone have ideas?
If the server stalls or the connection breaks (fails), that will happen. You will need to implement some type of timeout feature to prevent the endless loop.

edit: It could also be a port scanner checking the ports on your device. Some do not bother to close the connection after establishing one.

woodwax

#4
Aug 22, 2015, 12:40 pm Last Edit: Aug 22, 2015, 12:40 pm by woodwax
If the server stalls or the connection breaks (fails), that will happen. You will need to implement some type of timeout feature to prevent the endless loop.

edit: It could also be a port scanner checking the ports on your device. Some do not bother to close the connection after establishing one.

Thank You very much for the answers, I implement a very simple time out and its going very well.

Code: [Select]
while(!client.available())   {
    Serial.print(".");   
    delay(1);   
    timeout++;
    if(timeout>10000) {Serial.print("INFINITE LOOP BREAK!");  break;}
    }
    timeout=0;


Also I set NTP time refresh interval to about once per day.

Go Up