{"action":"created","issue":{"url":"https://api.github.com/repos/arduino/Arduino/issues/3829","labels_url":"https://api.github.com/repos/arduino/Arduino/issues/3829/labels{/name}","comments_url":"https://api.github.com/repos/arduino/Arduino/issues/3829/comments","events_url":"https://api.github.com/repos/arduino/Arduino/issues/3829/events","html_url":"https://github.com/arduino/Arduino/issues/3829","id":107417162,"number":3829,"title":"Reuse of EthernetClient instances don't work when check connected() before stop()","user":{"login":"Apollon77","id":11976694,"avatar_url":"https://avatars.githubusercontent.com/u/11976694?v=3","gravatar_id":"","url":"https://api.github.com/users/Apollon77","html_url":"https://github.com/Apollon77","followers_url":"https://api.github.com/users/Apollon77/followers","following_url":"https://api.github.com/users/Apollon77/following{/other_user}","gists_url":"https://api.github.com/users/Apollon77/gists{/gist_id}","starred_url":"https://api.github.com/users/Apollon77/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Apollon77/subscriptions","organizations_url":"https://api.github.com/users/Apollon77/orgs","repos_url":"https://api.github.com/users/Apollon77/repos","events_url":"https://api.github.com/users/Apollon77/events{/privacy}","received_events_url":"https://api.github.com/users/Apollon77/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"cmaglie","id":423515,"avatar_url":"https://avatars.githubusercontent.com/u/423515?v=3","gravatar_id":"","url":"https://api.github.com/users/cmaglie","html_url":"https://github.com/cmaglie","followers_url":"https://api.github.com/users/cmaglie/followers","following_url":"https://api.github.com/users/cmaglie/following{/other_user}","gists_url":"https://api.github.com/users/cmaglie/gists{/gist_id}","starred_url":"https://api.github.com/users/cmaglie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmaglie/subscriptions","organizations_url":"https://api.github.com/users/cmaglie/orgs","repos_url":"https://api.github.com/users/cmaglie/repos","events_url":"https://api.github.com/users/cmaglie/events{/privacy}","received_events_url":"https://api.github.com/users/cmaglie/received_events","type":"User","site_admin":false},"milestone":null,"comments":2,"created_at":"2015-09-20T20:53:26Z","updated_at":"2015-09-21T09:28:27Z","closed_at":null,"body":"Hey,\r\n\r\nI use a library where the EthernetClient instance is created once and then reused.\r\nThis was not really working - the first call worked, then it did not connect for addtional uses.\r\n\r\nI tracked it down to the following:\r\nThe library did the following:\r\n      /*if (udata->client->theClient->connected())\r\n        {\r\n          udata->client->theClient->stop();\r\n        }*/\r\n\r\nAnd 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.\r\nAnd that's why the \"conenct\" method found a still set \"_sock\" to a valid number and returned false.\r\n\r\nI found the advice to use \"if connected() then stop()\" in several sources in the internet, but this brings big problems as seen here ...\r\n\r\nI 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\r\n"},"comment":{"url":"https://api.github.com/repos/arduino/Arduino/issues/comments/141923643","html_url":"https://github.com/arduino/Arduino/issues/3829#issuecomment-141923643","issue_url":"https://api.github.com/repos/arduino/Arduino/issues/3829","id":141923643,"user":{"login":"Apollon77","id":11976694,"avatar_url":"https://avatars.githubusercontent.com/u/11976694?v=3","gravatar_id":"","url":"https://api.github.com/users/Apollon77","html_url":"https://github.com/Apollon77","followers_url":"https://api.github.com/users/Apollon77/followers","following_url":"https://api.github.com/users/Apollon77/following{/other_user}","gists_url":"https://api.github.com/users/Apollon77/gists{/gist_id}","starred_url":"https://api.github.com/users/Apollon77/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Apollon77/subscriptions","organizations_url":"https://api.github.com/users/Apollon77/orgs","repos_url":"https://api.github.com/users/Apollon77/repos","events_url":"https://api.github.com/users/Apollon77/events{/privacy}","received_events_url":"https://api.github.com/users/Apollon77/received_events","type":"User","site_admin":false},"created_at":"2015-09-21T09:28:27Z","updated_at":"2015-09-21T09:28:27Z","body":"The check in \"connect\" at the beginning is simply checking the current EthernetClient instance because it only checks that a valid Socket-number is assigned to the variable _sock (which states THE one socket used by EthernetCient at the moment). So it can't prevent \"missing stop() calls on intact connections\" cases ...\r\n\r\nSo my idea would be changing the \r\n\r\n/if (_sock != MAX_SOCK_NUM) return 0;/\r\n\r\ninto something like\r\n\r\n/if ( (_sock != MAX_SOCK_NUM) && connected(_sock) ) return 0;/\r\n"}}