From e4040df8576550181bca56831e84db3522a2ae56 Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Thu, 29 Jul 2021 09:37:14 +0200 Subject: [PATCH 1/2] Revert "Revert #50." This reverts commit 6f82133a90753edb8341269595380163df034459. --- arduino/libraries/WiFi/src/WiFiClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino/libraries/WiFi/src/WiFiClient.cpp b/arduino/libraries/WiFi/src/WiFiClient.cpp index 8b3d63ec..de183845 100644 --- a/arduino/libraries/WiFi/src/WiFiClient.cpp +++ b/arduino/libraries/WiFi/src/WiFiClient.cpp @@ -177,7 +177,7 @@ void WiFiClient::stop() uint8_t WiFiClient::connected() { if (_socket != -1) { - peek(); + available(); } return (_socket != -1); From e303e391d1e8b89d1849d66fd0fefcd74fb6b4c5 Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Thu, 29 Jul 2021 09:38:38 +0200 Subject: [PATCH 2/2] Call peek() when there are no available data to make sure socket gets closed when connection ends --- arduino/libraries/WiFi/src/WiFiClient.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arduino/libraries/WiFi/src/WiFiClient.cpp b/arduino/libraries/WiFi/src/WiFiClient.cpp index de183845..e5f05cbe 100644 --- a/arduino/libraries/WiFi/src/WiFiClient.cpp +++ b/arduino/libraries/WiFi/src/WiFiClient.cpp @@ -105,6 +105,7 @@ int WiFiClient::available() int result = 0; + //This function returns the number of bytes of pending data already received in the socket’s network. if (lwip_ioctl_r(_socket, FIONREAD, &result) < 0) { lwip_close_r(_socket); _socket = -1; @@ -150,6 +151,7 @@ int WiFiClient::peek() { uint8_t b; + //This function tries to receive data from the network and can return an error if the connection when down. if (lwip_recv_r(_socket, &b, sizeof(b), MSG_PEEK | MSG_DONTWAIT) <= 0) { if (errno != EWOULDBLOCK) { lwip_close_r(_socket); @@ -177,7 +179,10 @@ void WiFiClient::stop() uint8_t WiFiClient::connected() { if (_socket != -1) { - available(); + //Check if there are already available data and, if not, try to read new ones from the network. + if (!available()) { + peek(); + } } return (_socket != -1);