The WifiServer function available() only returns a client if two conditions have been met:
there is a client connected
that client has sent data that has not yet been read
I say this since the doc at WiFi - Arduino Reference states this explicitly and I find this to be the case in practice.
So, say I want to use a LED to indicate if a client is currently connected to the WifiServer.
How can I determine the status of a connection?
Once one has a client from say
WiFi client = WiFiServer.available();
one can look at client.connected()
but if there is no data awaiting to be read, there is no client object, and there is no WiFiServer.connected() function that I can see in the documentation.
Surely what is needed is:
WiFiServer.connected(); which would return a WifiClient (if connected even if no data is pending)
We could then look at WiFiClient.available() as needed.
As I see it, we cannot tell the difference between these two states:
client is connected, but no data has arrived or previous data has been read already
client is not connected
If a client connects, but never sends data, how can our code ever know that it is even connected?
Note this has nothing to do with HTTP which typically does not use persistent TCP connections and always send data.
It is the nature of the internet that every message is separate. As I type this the Arduino server is not waiting for a response and it does not care if it never gets one. When I press "Post" a connection will be made and the data will be sent.
And it may well be that what I send now goes to a different computer (on the same web address) from the one that sent this webpage to me.
Serial communication between a PC and an Arduino is the same.
Since I typically do send data when I connect to my ESP8266, this code tends to work.
I understand why the LED turns on when I connect (because I send data)
But it does not go off after the data has been read, when there should be no client (since no data has arrived). It does go off when the connection is closed, suggesting that WifiServer.available() does in fact NOT only return a client when data is awaiting in the ESP8266WiFi.h!
static WiFiClient client;
if (!client) {
client = RS.available(); // Doc: Gets a client that is connected to the server and has data available for reading.
// The connection persists when the returned client object goes out of scope
}
if (client) { // Doc: if no Client has data available for reading, this object will evaluate to false in an if-statement
if (client.available()>0) { // Doc: Returns the number of bytes available for reading
setAnalogLight(LED_BUILTIN,10); // turn on LED 10%
String line = client.readStringUntil('\r'); // read until a carriage return
handleRS(client,line); // handle it
yield; // I want other code to run
} //available
// } connected
}else{ //no client
setAnalogLight(LED_BUILTIN,0); // turn off LED
}; // client
DaleSchultz:
TCP connections can stay open and one can maintain persistent bidirectional communications until one side decides to close the connection.
I can understand that closing a connection prevents further communication. But keeping a connection open does not necessarily mean that communication will happen until you actually try it by sending a message. What happens if someone pulls the plug at the other end or some fool with a JCB digs up the communication cable.