-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Remote TCP disconnect not detected #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This patch works on ESP32 so the above program detects remote TCP disconnects. The errno value is ENOTCONN versus ECONNRESET which, I think, is used on BSD sockets API. I can submit a PR if it looks OK. diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 1090695..b1b6e72 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -279,8 +279,28 @@ void WiFiClient::flush() {
uint8_t WiFiClient::connected()
{
- uint8_t dummy = 0;
- read(&dummy, 0);
+ if (_connected) {
+ uint8_t dummy;
+ int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
+ if (res <= 0) {
+ switch (errno) {
+ case ENOTCONN:
+ case EPIPE:
+ case ECONNRESET:
+ case ECONNREFUSED:
+ case ECONNABORTED:
+ _connected = false;
+ break;
+ default:
+ _connected = true;
+ break;
+ }
+ }
+ else {
+ // Should never happen since requested 0 bytes
+ _connected = true;
+ }
+ }
return _connected;
} |
Yeah... I even raised an issue a while back for this. I guess they fixed it at some point. How about you PR this and the other change that you proposed? |
Hi @bbx10 , WiFiClient client = server.available(); if(client.available()){ // Read the first line of the request // Match the request } |
@bbx10 Chuck |
I have the same problem with all series of ESP when the client disconnect, the server freezes in the last situation. Even when the client re connected. |
The test program works on ESP8266 but fails on ESP32. The code below listens on TCP port 9 and discards all incoming data. On a Linux box, use netcat (nc 9) to connect to the ESP. Pressing ^C stops netcat and terminates the TCP socket. On ESP32, the sketch never detects the remote TCP disconnect. On ESP8266, remote TCP disconnects are detected correctly.
BTW, the ESP32 TCP Rx performance is much slower than the ESP8266 but perhaps the Rx window size needs tweaking or something. I will open another issue if this is a real problem.
The text was updated successfully, but these errors were encountered: