I'm using WeMos D1 R2. I'm trying to listen to both ports 1992 and 1993. This is my current code: #include
WiFiServer server_1992(1992);
WiFiServer server_1993(1993);
void setup() {
Serial.begin(9600);
WiFi.begin("MySSID", "MyPassword");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println(" connected");
server_1992.begin();
server_1992.setNoDelay(true);
server_1993.begin();
server_1993.setNoDelay(true);
}
void check_server(WiFiClient* client, WiFiServer* server, const char* text){
if (client->connected()) {
if (client->available()){
unsigned char c[250];
client->readBytes(c, 1);
Serial.print(c[0]);
Serial.print(" ");
}
} else {
client->stop(); //Adding it here like Juraj said. Not tested.
*client = server->available();
if (*client) Serial.print(text);
}
}
void loop() {
WiFiClient client_1992, client_1993;
while (1){
check_server(&client_1992, &server_1992, "1992 connected \n");
check_server(&client_1993, &server_1993, "1993 connected \n");
}
}
The problem is when the client(s) disconnect from the hotspot, my code keeps waiting for data forever without trying to acquire new client (with client = server->available();
).
Solution: for now I ask the client "Hey are we still connected?" every few seconds and manually kick the client out if it doesn't reply. More on this
Sorry if it's wrong to link other question here, I just hope people who have the same problem as mine can find the solution.