Infinite loop at while(!client.available())

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:

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.

sketch_aug21a.ino (4.74 KB)

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

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

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.

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.

SurferTim: 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.

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.