(client.connected()) and(client.available()) counter disparity

I've been trying to detect unusual goings on in my programs and started adding counters in all the sections to show me what is happen and came across this weird thing.

If you run the below you will notice that the counter connected available RAPIDLY runs crazy while the other two act expectedly.

As an example, here is what I have after a few clicks on the screen
ifClient is 29

ClientCn is 379350

ClientAvl is 12688

However, you uncomment out the Serial.println("ClientCn"); and Serial.println("ClientAvl"); guess what, the numbers are more normal looking.

My guess is there must be some delay somewhere but why and how is this fixed. Plus I think it might be tripping the watchdog timer.

Another anomaly is that the ifClient counter always increments by two.

Any ideas?

Thanks for looking. Device is esp8266

#include <ESP8266WiFi.h>
const char ssid[]       = "whatever";
const char password[]   = "whatever";
uint32_t ifClient = 0;
uint32_t ClientAvl = 0;
uint32_t ClientCn = 0;
WiFiServer server(80);
void setup() {
  Serial.begin(115200);
  Serial.println("hello");
  wificonnect();
  Serial.println(WiFi.localIP());
}
void loop() {
  WiFiClient client = server.available();
  if (client) {
    ifClient++;
    Serial.println("ifClient");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      ClientCn++;
      //Serial.println("ClientCn");
      if (client.available()) {
        ClientAvl++;
        //Serial.println("ClientAvl");
        char c = client.read();
        Serial.write(c);
        // send a standard http response header
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");   
          client.println("Refresh: 85");    
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("<br>&nbsp;&nbsp;<span style=\"font-size: 26px\";>ifClient is ");
          client.println(ifClient);
          client.println("</span><br />");
          client.print("<br>&nbsp;&nbsp;<span style=\"font-size: 26px\";>ClientCn is ");
          client.println(ClientCn);
          client.println("</span><br />");
          client.print("<br>&nbsp;&nbsp;<span style=\"font-size: 26px\";>ClientAvl is ");
          client.println(ClientAvl);
          client.println("</span><br />");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
    Serial.println("   client.stop   ");
  }
}

void wificonnect() {
  uint8_t wifi_retry = 0;  // COUNTER SOLVES ESP32-BUG WITH CERTAIN ROUTERS: CONNECTION ONLY ESTABLISHED EVERY SECOND TIME
  yield();
  while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_retry < 3) {
    WiFi.begin(ssid, password);
    delay(3000);
    wifi_retry++;
  }
  if (wifi_retry >= 3) {
    ESP.restart();
  }
  yield();
  if (WiFi.waitForConnectResult() == WL_CONNECTED) {
    server.begin();
    Serial.println("WiFi connected");  
  }
}

the print slows it down. that is all

1 Like

So between connected and available there is just nothing "available"?

Yes. You are checking the client for available data thousands of times per second. Sometimes it just doesn't have any data for you. You read one character each time you see that a character is available so the "ClientAvl" count will be the same as the number of characters received. The "ClientCn" count will include all of those plus any time you found the client still connected but it didn't have a character available.

1 Like

WiFiNINA or Ethenet library and other libraries with the right server.available() implementation return only a client with data already available.
The esp8266 and esp32 server.available() works as Ethernet library's server.accept()
https://www.arduino.cc/en/Reference/EthernetServerAccept
If you want the right server.available() on esp8266, you can use ArduinoWiFiServer as shown in PagerServer example.
https://www.arduino.cc/en/Reference/ServerAvailable

the Arduino WiFi101 library server.available() has a hiccup too. it returns a just connected client even it doesn't have data available, but other connected clients are returned only if they have data available.

1 Like

It's amazing how fast that counter climbs. At first I thought I had some infinite loop somewhere.

Not infinite, but repeating very quickly whenever there is no character available:

  while (client.connected()) 
  {
      ClientCn++;
      //Serial.println("ClientCn");
      if (client.available()) { }
  }

[/quote]

makes me wonder how the watchdog can determine which is a good loop from a bad loop. lol.

it doesn't take the 4 seconds after which watchdogs resets.
and calling WiFi library function calls the SDK, which then can handle things and reset the watchdog.

Wow. How do you know all this stuff? Wish I did.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.