-->
Page 1 of 3

ESP8266WiFi server.available() disconnects the client

PostPosted: Fri May 08, 2015 5:00 am
by Lord Ivanhoe
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

Re: ESP8266WiFi server.available() disconnects the client

PostPosted: Sat May 09, 2015 10:04 am
by icserny
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.

Re: ESP8266WiFi server.available() disconnects the client

PostPosted: Sat May 09, 2015 10:52 am
by MicKeyCZ
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

Re: ESP8266WiFi server.available() disconnects the client

PostPosted: Tue May 12, 2015 6:08 am
by Lord Ivanhoe
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