Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFiClientSecure: connection timeout not working #5398

Closed
vlastahajek opened this issue Jul 16, 2021 · 0 comments · Fixed by #5418
Closed

WiFiClientSecure: connection timeout not working #5398

vlastahajek opened this issue Jul 16, 2021 · 0 comments · Fixed by #5418

Comments

@vlastahajek
Copy link
Contributor

vlastahajek commented Jul 16, 2021

When connecting to not responding server/port using WiFiClientSecure , connection timeout is always around 18s.
However, connection timeout works ok for the WiFiClient.

When looking at sources, WiFiClient::connect uses a common non-blocking socket connect pattern for receiving the desired timeout. This is not implemented in the start_ssl_client function used by WiFiClientSecure.

Is there a reason, why connection timeout is not implemented in the start_ssl_client function?

I've tried copy the initial connection pattern from WiFiClient::connect pattern to start_ssl_client and it works ok (unsurprisingly).

Testing code:

#include <WiFiClientSecure.h>

 const char* ssid     = "SSID ";     // your network SSID (name of wifi network)
 const char* password = "password"; // your network password

const char*  server = "www.howsmyssl.com";  // Server URL



void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  delay(100);

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  // attempt to connect to Wifi network:
  int i = 30;
  while (WiFi.status() != WL_CONNECTED && --i) {
    Serial.print(".");
    // wait 1 second for re-trying
    delay(500);
  }
  if(WiFi.isConnected()) {
    Serial.print("Connected to ");
    Serial.println(ssid);
  } else {
    Serial.println("Connection failed ");
    Serial.println("Restarting.. ");
    ESP.restart();
  }

  WiFiClientSecure secClient;


  secClient.setInsecure();

  Serial.println("\nStarting secure connection to server...");
  uint32_t start = millis();
  int r = secClient.connect(server, 440, 5000);
  Serial.printf("Connection took: %lums\n", millis()-start);
  if(!r) {
    Serial.println("Connection failed!");
  } else {
    Serial.println("Connected!");
    secClient.stop();
  }

  WiFiClient client;
  Serial.println("\nStarting unsecure connection to server...");
  start = millis();
  r = client.connect("192.168.1.3", 500, 3000);
  Serial.printf("Connection took: %lums\n", millis()-start);
  if(!r) {
    Serial.println("Connection failed!");
  } else {
    Serial.println("Connected!");
    client.stop();
  }
}

void loop() {
  // do nothing
}
vlastahajek added a commit to bonitoo-io/arduino-esp32 that referenced this issue Jul 19, 2021
vlastahajek added a commit to bonitoo-io/arduino-esp32 that referenced this issue Jul 19, 2021
me-no-dev pushed a commit that referenced this issue Jul 21, 2021
Closes #5398

Using the same non-blocking socket connect pattern for respecting connection timeout, copied from WiFiClient::connect.

WiFiClient::connect uses lwip_connect_r, whereas start_ssl_client uses lwip_connect. I haven't found what is the difference between them. I tested both, both work ok, so I kept lwip_connect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant