Reuse of EthernetClient instances don't work when check connected() before stop() #7
Description
From @Apollon77 on September 20, 2015 20:53
Copied from original issue: arduino/Arduino#3829
I use a library where the EthernetClient
instance is created once and then reused.
This was not really working - the first call worked, then it did not connect for additional uses.
I tracked it down to the following:
The library did the following:
if (udata->client->theClient->connected())
{
udata->client->theClient->stop();
}
And this was the reason! After all data was received the called server closed the connection. So the socket was in status CLOSE_WAIT
and available
returned 0
because all data was already read. That's why stop
was never called.
And that's why the connect
method found a still set _sock
to a valid number and returned false
.
I found the advice to use "if connected()
then stop()
" in several sources in the internet, but this brings big problems as seen here.
I think the fix should be to change the check in connected
: All Sockets that are in CLOSE_WAIT
are reused in any case ... so a call to connect
should check also connected
and call stop
or simply reuse that socket also when _sock
is a value < MAX_SOCK_NUM
.