WifiClient setTimeout() not working

greggm
Posts: 9
Joined: Sun May 21, 2017 10:44 am

WifiClient setTimeout() not working

Postby greggm » Thu Sep 28, 2017 9:49 pm

I have setup a quite simple and standard wifi client waiting for data, and wanted to test how setTimeout() would work, but i dont get this working.
This is the esp32 arduino code:

Code: Select all

...
WiFiServer server(port);

void serverStartup(){
  server.begin();
  //server.setTimeout(5);
  delay(1000);
  Serial.println("Waiting for client...");

  WiFiClient client = server.available();

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    while (client.connected()) {            // loop while the client's connected
      client.setTimeout(5);                  // timeout does not seem to work
      //Serial.print("Client connected");
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        printf("%d",c);                    // print it out the serial monitor
      }
    }

    client.stop();
    Serial.println("Client Disconnected.");
  }
  Serial.println("Ending server.");
  server.end();
}

void setup()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    WiFi.config(myIP,gateway,subnet);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop()
{
    delay(1000);
    serverStartup();
}
and i am feeding the esp32 server with a simple javascript/node.js client:

Code: Select all

var net = require('net');

var client = net.connect({ host: "192.168.1.127", port: 1234 }, doNothing);

function doNothing(){
  console.log("Doing nothing.");
  
The client does connect, but sends nothing:

Code: Select all

$ node simpleclient.js
Doing nothing.
While the esp32 server is listening:

Code: Select all

WiFi connected
IP address:
192.168.1.127
Waiting for client...
Ending server.
Waiting for client...
Ending server.
Waiting for client...
New Client.
But after that nothing happens anymore. In other words the connection stays open until it is closed by the client or the client is killed.
I would have expected that the esp32 server disconnects after the timeout of 5 seconds.

I tried out various things like setting the timeout on the server (which has the same effect) or just after the client was created (which resulted in an error (because the client was not connected?). I also checked that the return code of setTimeout is non-negative.
But i never saw a timeout.

In fact this looks very similar to https://github.com/espressif/arduino-esp32/issues/330, but that issue seems to be closed...
(I am using platformio on visual studio code and have just updated esp32 arduino just today to 0.10.0)

Any suggestions?

Gregg

Who is online

Users browsing this forum: No registered users and 82 guests