• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 WiFiClient.connected() says client is connected, while the client is long gone

Q&A Forum › Category: ESP32 › ESP32 WiFiClient.connected() says client is connected, while the client is long gone
0 Vote Up Vote Down
Bernie asked 3 years ago

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
}
Question Tags: WiFi ESP32
5 Answers
0 Vote Up Vote Down
Best Answer
Sara Santos Staff answered 3 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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

0 Vote Up Vote Down
Bernie answered 3 years ago

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?

0 Vote Up Vote Down
Bernie answered 3 years ago

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.

1 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Great!
I’m glad this helps.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.