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.mode(WIFI_AP);
WiFi.softAP("test");
WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));
Serial.println(WiFi.localIP());
server.begin();
}
void loop()
{
while (!client)
{
client = server.available();
delay(10);
}
while (client.connected())
{
Serial.println("ok");
delay(1000);
client.read();
}
}
C# code:
static void Main(string[] args)
{
TcpClient client = new TcpClient("192.168.4.1", 222);
client.Close();
}
-
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– SDsolar2017年06月03日 11:07:04 +00:00Commented 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– schacker222017年06月04日 11:47:50 +00:00Commented 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– SDsolar2017年06月04日 23:48:43 +00:00Commented 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– schacker222017年06月04日 11:56:43 +00:00Commented Jun 4, 2017 at 11:56 -
Whether you're using the WiFiEsp library is not a question I know the answer to.curryspice– curryspice2017年06月06日 06:05:17 +00:00Commented Jun 6, 2017 at 6:05
-
I've seen the client.connected() function in WiFiEsp onlycurryspice– curryspice2017年06月06日 06:06:09 +00:00Commented Jun 6, 2017 at 6:06
-
This one's
WiFiEspClient::connected()
and notWiFiClient::connected()
schacker22– schacker222017年06月10日 19:23:25 +00:00Commented 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.
Context
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– schacker222017年06月20日 17:44:33 +00:00Commented Jun 20, 2017 at 17:44