TCP Client not reading anything

I just want to report back a minor success. Remember in the early days we all noticed that TCPClient.available() was returning a boolean rather than a count of bytes? Well the recent changes fixed this and it must be related to the TCPClient problems.

My old code for the boolean case looked like this and extracted one byte per call of loop():

while (myTCP.available()) {
    serialEvent();
}

This code stopped working sometime last night when the new TCPClient rolled out.

Now my code looks like this:

int bytecount = myTCP.available();
while (bytecount>0) {
    serialEvent();
    bytecount--;
}

And this works much better.

I thought I was being a good cooperative-multitasking doobie by giving control back to the TCPClient every byte, but I think it does not like that! Now if the available() method says there are N bytes available, I process them all at once and only then give control back by letting loop() go round again. Maybe this prevents premature buffer reuse in the TCPClient, I don't know.

But it does work much better now--maybe you guys can try it too.

1 Like