ESP8266 - TCP server with multiple clients

Hello,

I'm testing a way to send & receive hexadecimal commands from wifi <-> serial with an ESP8266.
The sketch allows multiple connections and works perfectly.

The only issue is that it works for a few hours and then doesn't accept any new connection anymore?!

If I leave the "logging" serial prints I can see that each client fills up all available ports when it first connects. But....it works.

Would any of you have an idea ???
The goal is to have just 2 TCP clients active on the same ports.

#include <ESP8266WiFi.h>

//how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 2
const char* ssid = "SSID";
const char* password = "pwd";

WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];

void setup() {
//Serial.begin(9600);

IPAddress ESP8266_ip ( 192, 168, 0, 155);
IPAddress dns_ip ( 192, 168, 0, 1);
//IPAddress dns_ip ( 8, 8, 8, 8);
//IPAddress dns_ip ( 192, 168, 0, 0);
IPAddress gateway_ip ( 192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);

WiFi.config(ESP8266_ip, gateway_ip, subnet_mask);
//WiFi.config(ESP8266_ip, gateway_ip, subnet_mask, dns_ip);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//Serial.print("\nConnecting to "); Serial.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(100);
if(i == 21){
//Serial.print("Could not connect to"); Serial.println(ssid);
while(1) delay(500);
}
//start UART and the server
Serial.begin(9600);
server.begin();
server.setNoDelay(true);

//Serial.print("Ready! Use 'telnet ");
//Serial.print(WiFi.localIP());
//Serial.println(":23' to connect");

}

void loop() {
uint8_t i;
//check if there are any new clients
if (server.hasClient()){
for(i = 0; i < MAX_SRV_CLIENTS; i++){
//find free/disconnected spot
if (!serverClients || !serverClients*.connected()){*
if(serverClients_) serverClients*.stop();
serverClients = server.available();
// Serial.print("New client: "); Serial.print(i);
continue;
}
}
//no free/disconnected spot so reject*

* WiFiClient serverClient = server.available();
serverClient.stop();
}
//check clients for data*

* for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients _&& serverClients.connected()){*

_ if(serverClients*.available()){
//get data from the telnet client and push it to the UART*

while(serverClients.available()) Serial.write(serverClients*.read());
}
}
}
//check UART for data*

* if(Serial.available()){_
size_t len = Serial.available();
uint8_t sbuf[len];
_ Serial.readBytes(sbuf, len);
//push UART data to all connected telnet clients*

* for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients _&& serverClients.connected()){*

_ serverClients*.write(sbuf, len);
delay(100);
}
}
}
}*_

"If I leave the "logging" serial prints I can see that each client fills up all available ports when it first connects. But....it works."

You can correct it if you replace "continue;" with "break;"

Some typos: You must replace "serverClients" with serverClients (bracket) i (bracket)

1 Like