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 

Activity

added a commit that references this issue on Dec 3, 2018

espressif#2147 available() shouldn't return 0 after disconnect if the…

added a commit that references this issue on Dec 4, 2018

#2147 available() shouldn't return 0 after disconnect if there is sti…

stale

stale commented on Aug 1, 2019

@stale

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale

stale commented on Aug 15, 2019

@stale

This stale issue has been automatically closed. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      WiFiClient.available() returns 0 after disconnect, even if there is data still in buffer · Issue #2147 · espressif/arduino-esp32