I use an ESP32 to open an access point and listen for TCP connections over a WiFiServer object.

When I get a client on the server I want to know when it has disconnected.

Currently I have a simple piece of code on both the client and the server side. I connect using a TcpClient in C#.

When I call client.close() on the client side, the client.connected() loop on the server just continues forever.

Here is the ESP code:

void setup()


   WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));



void loop()
    while (!client)
        client = server.available();


    while (client.connected())



C# code:

static void Main(string[] args)
     TcpClient client = new TcpClient("", 222);

  • Welcome to Arduino SE. I don't really see a question here. I see a statement of the problem but no attempts at resolution nor a question about why such an attempted fix didn't work. I recommend you take the tour at arduino.stackexchange.com/Tour to get the most out of this site. Perhaps you can flesh it out enough to prevent closure of this one. Or, if it does get closed, perhaps you can repost it with the elements necessary to present a question that can attract useful answers.
    – SDsolar
    Jun 3, 2017 at 11:07
  • @SDsolar I'm absolutely aware on how to write on this site, because I'm on stackoverflow as well. Furthermore my question is clear when you read the title and the second line. I'm explaining my intention there and with a little bit of think you can guess that I need help with this function. Instead of writng pointless comments spend the time writing some answers.
    – schacker22
    Jun 4, 2017 at 11:47
  • I just don't see a question here, that's all. This is a Q&A site. Without one you can't have the other. I voted to leave this one open when it came up in review, thinking a little nudge was all you needed.
    – SDsolar
    Jun 4, 2017 at 23:48

2 Answers 2


This is a bug in a previous version of the library as mentioned on https://github.com/bportaluri/WiFiEsp/issues/14

You can alternatively use WiFi.softAPgetStationNum() to get the count of stations connected to the AP interface.

  • WiFiEsp isn't the library I'm using, is it? Unfortunately WiFi.softAPgetStationNum() does't help me, because I need to know when the socket has disconnected and not a Station.
    – schacker22
    Jun 4, 2017 at 11:56
  • Whether you're using the WiFiEsp library is not a question I know the answer to.
    – curryspice
    Jun 6, 2017 at 6:05
  • I've seen the client.connected() function in WiFiEsp only
    – curryspice
    Jun 6, 2017 at 6:06
  • This one's WiFiEspClient::connected() and not WiFiClient::connected()
    – schacker22
    Jun 10, 2017 at 19:23

My actual answer

You can try to inquire client.status() as a workaround. When no one is connected it should return 0.


What are you building on? I tried to reproduce your problem but everything works as expected, the serial stops printing "ok" when I disconnect.

Also, what libraries are you using exactly? I included ESP8266WiFi.h and WiFiClient.h.

I used platformio v3.4 on an ESP8266-01 module and a simple Android netcat app as client.

  • 1
    ESP8266WiFi.h is for an ESP8266 which is the wrong plattform, I have an ESP32 Development Board. I included #include <WiFi.h>.
    – schacker22
    Jun 20, 2017 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.