I am connecting to WiFiClient from a Putty terminal emulator on Windows. After a short while the connection invariably freezes and Putty becomes unresponsive. The serial monitor shows that client.connected() continues to say it is still connected to the terminal, long after the terminal freezes and is closed. Monitor also shows that WiFi.status remains WL_CONNECTED throughout. The code works well, except that after a few minutes client.available() and client.write() do nothing while client.connected() claims there is a connection, and execution remains in the while loop.
This prevents the terminal from reestablishing a new connection. Is this a known issue, or is there something wrong with the code below. In the code below Serial.print() statements have been removed.
#include <WiFi.h> const char* ssid = "ssid"; const char* password = "password"; WiFiServer server(23); WiFiClient client; void setup() { Serial.begin(115200); Serial2.begin(9600); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) delay(500); server.begin(); } void loop() { uint8_t Xbuf[256]; delay(100); client = server.available(); // listen for incoming clients if (!client) return; while (client.connected()) { // client connected, listen for input if (client.available()) Serial2.write(Xbuf, client.read(Xbuf, 256)); if (Serial2.available()) client.write(Xbuf, Serial2.readBytes(Xbuf, 256)); delay(100); } client.stop(); // client disconnected }
Hi.
I don’t know.
I’m not sure why that happens.
But I found some people with similar issues.
Maybe the following discussions might help:
- https://www.esp32.com/viewtopic.php?t=2386
- https://stackoverflow.com/questions/57089235/esp32-wificlient-connect-always-return-true
In the first discussion, they provide some code examples. I hope this helps.
Regards,
Sara
Hi.
I’m not sure that I really understand the issue.
Is your example based on a specific project?
Does the ESP32 connect via Wi-Fi to your router?
Regards.
Sara
Yes, the ESP32 connects to a WiFi access point, receives strings from a telnet (port 23) TCP connection, and relays the string to Serial2 which is connected to a Mega, and vice versa. The idea is to give the Mega a WiFi capability.
But when the connection freezes, client.connected() does not recognize that and continues to report that the client is still there, even if the client is closed. Why does the TCP connection freeze and why does client.connected() not report that the client is disconnected?
That’s very helpful Sara. I believe it explains the problem.
I was also thinking of sending a null message from the server to the client every few minutes, because that seems to keep the connection alive, as something seems to be timing out. This is why I don’t like TCP which requires a connection. UDP on the other hand is connectionless and a lot less problematic and much more efficient. When the internet used to be very unreliable, I guess TCP made sense. But not anymore.
I don’t think you will have this problem on a web server because HTTP which is based on TCP is transient. Once the data is sent to the client, the TCP connection I believe is closed. The next HTTP request opens a new TCP connection. These connections don’t last for more than a few seconds, and if there is a timeout that causes the freeze, the connection is just too short to timeout. If the connection remains open, new data is continuously being transmitted, which keeps the connection alive. Also with a web server, the server is multi-client, so if one client freezes, another can still connected. In my case, there can be just one client.