Skip to content

WiFiClient.available() returns 0 after disconnect, even if there is data still in buffer #2147

Closed
@rdowning-triax

Description

@rdowning-triax

Hardware:

Board: ESP32 Gateway
Core Installation/update date: latest dev
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: ?no?
Upload Speed: 115200
Computer OS: Windows 10

Description:

When trying to stream a wificlient request content to serial, the end of the request data is lost because available() returns 0 once the client disconnects, even if there is still data to be read from the buffer. When running the program below, you can see the output never gets all the way to the end "</html>" tag.

Sketch: (leave the backquotes for code formatting)

#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <WiFi.h>

//#define USE_SECURE
#ifdef USE_SECURE
#define PORT 443
#else
#define PORT 80
#endif

void testClient(const char * host, uint16_t port)
{
  Serial.print("\nconnecting to ");
  Serial.println(host);

#ifdef USE_SECURE
  WiFiClientSecure client;
#else
  WiFiClient client;
#endif
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
  client.setTimeout(1000);
  client.print("GET / HTTP/1.0\r\n\r\n");
  while (client.connected() || client.available()) {
    if (client.available()) {
      Serial.write(client.read());
    } 
//    else {
//      Serial.println("F");
//    }
  }

  Serial.println("\n\nclosing connection\n");
  client.stop();
}

void setup()
{
  Serial.begin(115200);
  WiFi.begin(NETWORK, PASSWORD);
  Serial.print("connecting to wifi");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

}


void loop()
{
  testClient("google.com", PORT);

  delay(10000);
}

Debug Messages:

Enable Core debug level: Debug on tools menu of Arduino IDE, then put the serial output here 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions