Help with client.read() useage

I have crated a test program using the TCPServer example.
I need a bit of help understanding client.read();
In the example it has server.write(client.read()); which i get, so then i changed it to this.
Serial1.print((char)client.read()); which works just fine.

what puzzles me is if i do the following I will get how many bytes it received
int z;
z = client.read();
Serial1.print(z);

So then why doesn’t Serial1.print((char)client.read()); print the bytes received ? It prints the received data.

what i am trying to do is take how many bytes received and throw that into a buffer.

int z;
uint8_t temp[512];
z = client.read(); // how much data did we get.
memcpy(temp, ??, z); // i do not know how to get to the received data buffer

Hi @seulater

I think you are confusing client.read() with client.available().

client.available() tells you how many bytes are available to be read.

client.read() reads each byte in succession and returns -1 if there are no more bytes as a convenience. Try something like this (I didn’t get chance to test this):

uint32_t bytesAvail = client.available();
uint32_t ptr=0;
uint8_t temp[512];
while(bytesAvail>0) {
  temp[ptr++] = client.read();
  bytesAvail--;
}
// if temp is char string, term it here temp[ptr] = '\0';

@bko thank you, but i was in error.
I though that it returned more than one byte and i was trying to get to its pointer location.
I see now that it only returns one byte at a time.

1 Like

I think you are are thinking of UDP where after UDP.parsepacket() you can call UDP.read(packetBuffer,size). TCP is different (for no reason that I can think of).

1 Like