Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By Lord Ivanhoe
#16876 This line
Code: Select allif (server.available()>0)

or even
Code: Select allSerial.print(server.available())


will actually accept the connection and disconnect the client immediately if there was one waiting. Is this intended, and, if so, is there any other way to know if there is a client waiting?

Thnx
Ivan
User avatar
By icserny
#16985 In the webserver example (WiFiWebserver.ino) this function is used in another way:
Code: Select all // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();


It seems to me, that the correct use of the server.available() method is to create a new client connection only. After that you should use the client.available() method to check if anything was sent already by the client.
User avatar
By MicKeyCZ
#16992 Hi,
server.available() returns WiFiClient that is destroyed (and disconnected) after evaluating your condition.

Use
Code: Select allWiFiClient client = server.available();
if (client) { // operator bool(), or if appropriate client.connected() or client.status()

to check if client is available.

M
User avatar
By Lord Ivanhoe
#17234 True, but there is another problem with that. Unlike ethernet lib, calling WiFiClient client = server.available() will disconnect any previously connected clients.
But i figured out how to make multi-client tcp server another way and it works like a charm, so i guess everything is good :)

Anyways, thanks for the quick responses :)

Ivan