TCPClient.available() does not return length

Acording to the Doc, this method should return the length of the response.

But actually it only returns 0 or 1.

also in the source-code I don’t see a return for the length.

sample code:

TCPClient client;
byte server[] = {127,0,0,1};

/* This function loops forever --------------------------------------------*/
void loop() {
	  if ( nextTime > millis() ) {
      Serial.println("connecting to server...");
      if (client.connect(server, 80))  {
        client.println("GET /asdf/ HTTP/1.1");
        client.println("HOST: localhost.local");
        client.print("Connection: close\n");

        Serial.println("sent request");
      } else  {
        Serial.println("connection failed");
        nextTime = millis() + 5*1000;

        //wait for request to be processed.

    Serial.println("waiting for response....");
    int len = client.available();


connecting to server…
sent request
waiting for response…

Hi @Coffee,

You’re totally right! Either the documentation is wrong, or the function should be updated, I’ll add that to our bug tracker. In the meantime you could always do a read with something like:

int bufSize = 256;
byte buffer[256];
int idx = 0;

while (client.available()) {
  if (idx >= bufSize) { break; }
  buffer[idx++] =;
Serial.print("Msg was ");
Serial.println(" bytes ");

(Note, I didn’t test this code)

Hi, thanks for clarifying.

I have a related question: in my project

i have a delay(1000) before calling client.avalable(). is this necessary and useful, or bad?


The following commit should fix the TCPClient.available() issue:

1 Like

look at the following example:

it works when using a delay(2000) before reading the response. without the delay it doesn’t work. any idea how to make the coder better?

Can someone please post a complete http example?

Hmm - what happens when you don’t have the delay? It’s possible that calling client.available()before client.connect() has completed (on the CC3000 side) might cause an issue; if so, we can put in a safeguard against that.

without delay, available() returns 0.

It’s because the client.available() has no data yet, i.e. your request is taking longer than 1 second, but less than 2 seconds. When that happens your “bytes” == 0 so your for() loop gets skipped and your client.stop() is run.

On the surface, better code would be instead of the delay(2000);

// wait 5 seconds or less for the server to respond
uint32_t startTime = millis();
while(!client.available() && (millis() - startTime) < 5000);

It might be possible to put this functionality into the client.available() method… but would you really want it in there?

EDIT: I tried this in your code and it works great!

1 Like