Is there a limit on the number of WiFiServers and Clients one can use (ESP8266)?

I have a working system based on ESP-07S where the purpose of it is to connect a serial port to a network client. So the software implements a TCP_UART bridge based on sample code. This works fine now.

In order to debug the data flow between the master and the serial equipment I want to add a logging function to the ESP system. The idea is that I would start two additional TCP servers on the ESP on port numbers close to the bridge port and manage the client connections much the same way as the bridge client. When there are logging clients connected these would receive a copy of the data received by the ESP on the bridge connection (TCP) and the serial line.

My problem is that when I had implemented this code (switched on/off by a conditional define) and loaded it to the ESP it totally crashed on startup with a bunch of exception messages on the debug serial port... I had to undefine the LOGDATA symbol and recompile the sketch and then upload it using the serial port since the webupdater did not work because the device restarted in a tight loop. This got me back to the pre-logging state OK.

This got me wondering if I have reached some limit on the number of simultaneously running TCP servers the ESP is able to manage?

What I have in terms of TCP servers is:

    WiFiServer *tcpServer;  //Instantiated later in setup() when the port is known
    WiFiClient tcpServerClient;
....
    tcpServer = new WiFiServer(ESPConf.tcpport);

The sketch also contains the firmware WebUpdater code using two objects:

    ESP8266WebServer httpServer(80);
    ESP8266HTTPUpdateServer httpUpdater;

Then I have also added a webserver to handle the device configuration and this allows for client connections from my configuration utility:

    WiFiServer *tcpConfigSrv; //This server is used to configure the module using simple AT-like commands
    WiFiClient tcpConfigClients[MAX_CONF_CLIENTS];
   ....
    tcpConfigSrv = new WiFiServer(CONFIG_PORT);

So at this state I have the following 3(or 4) servers running: The firmware update service: - httpServer - httpUpdater

The TCP to Serial bridge service: - tcpServer

The configuration server: - tcpConfigSrv

With LOGDATA defined these are also added:

#ifdef LOGDATA
    WiFiServer *logServerSS, *logServerSSM;    //The logging servers for the two data directions instantiated in setup()
    WiFiClient logClientSS, logClientSSM;    //The client connections to the log servers
#endif
...
#ifdef LOGDATA
    void StartLogDataServers()
    {
        // Instantiate and start TCP data logging servers on port TCP_PORT + 10 and 11
        //Data originating from serial line
        logServerSS = new WiFiServer(TCP_PORT + 10);
        logServerSS->begin();
        logServerSS->setNoDelay(true);

        //Data originating from Master App via TCP socket
        logServerSSM = new WiFiServer(TCP_PORT + 11);
        logServerSSM->begin();
        logServerSSM->setNoDelay(true);

        SerialDebug.print("Debug data server ready! Use ports ");
        SerialDebug.print((TCP_PORT +10));
        SerialDebug.print(" and ");
        SerialDebug.println((TCP_PORT +11));
    }
#endif

With these present the ESP does not reach a stable startup state but crashes with exceptions... So I will not even reach loop() where the connection state is checked.

Is there a limit on the number of TCP servers on ESP8266 or do I have to look elsewhere for the reason for the device crash???

How do you set the MAC address?

yes. there is a limit 4 connection.

Does this 4 instance limit pertain to the servers or the clients?
I.e. can there be more than 4 servers listening as long as no more than 4 clients are connected?

And I don’t really know if the WebUpdater runs 1 or 2 services…
Here is what defines and starts the webupdater:

....
#ifdef WEBUPDATE
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include "ESP8266HTTPUpdateServer.h"
#endif
....

#ifdef WEBUPDATE
    const char* update_path = "/firmware";
    const char* update_username = "updateuser";
    const char* update_password = "updatepwd";
    ESP8266WebServer httpServer(80);
    ESP8266HTTPUpdateServer httpUpdater;
#endif

void setup()
{
    ....
    #ifdef WEBUPDATE
      httpUpdater.setup(&httpServer, update_path, update_username, update_password);
      httpServer.begin();
    #endif
    ...
}

void loop()
{
    HandleSerialServer();
    HandleConfigServer();

    //Check for firmware update request
    #ifdef WEBUPDATE
      httpServer.handleClient();
    #endif
}

I need the uart_bridge, configuration and webupdater services for basic functionality.
To do logging I figured I would need an extra 2 services, one to log TCP inbound data and one to log serial output data…
This is because the monitored data are binary, not text…

sorry, it is 5 The max number of connection is determined by options of lwip at compile time. see file esp8266/hardware/esp8266/2.4.2/tools/sdk/lwip2/include/lwip/opt.h

/**
* MEMP_NUM_TCP_PCB: the number of simultaneously active TCP connections.
* (requires the LWIP_TCP option)
*/
#if !defined MEMP_NUM_TCP_PCB || defined __DOXYGEN__
#define MEMP_NUM_TCP_PCB                5
#endif

/**
* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP connections.
* (requires the LWIP_TCP option)
*/
#if !defined MEMP_NUM_TCP_PCB_LISTEN || defined __DOXYGEN__
#define MEMP_NUM_TCP_PCB_LISTEN         8
#endif

Thanks, then since that limitation presumably is about [u]active connections[/u] it seems like it should limit the connectivity of external clients, right? I.e. if there are already 4 clients connected to various servers then one additional client could be connected without hitting the magic number. But with [u]no clients connected at all[/u] my sketch crashes with these exceptions [u]already on startup[/u], so I guess that the problem lies elsewhere.... And the allowed connected clients seem to be 8 so this should not be a problem. Or maybe I am not understanding the file you posted? Max Listening ports = 8 Max Active connections = 5 In my case I would have just the bridge client and two log clients connected...

Then I need to add more debug messages and check exactly where it crashes. It is a pain since the serial flashing is awkward because of the way the board is built without easy access to the necessary pins. :o

UPDATE: Turns out that my StartLogServer() function was never called so in loop() I accessed non-initialized objects... Now fixed and working.

Thanks for the info about the limitations in numbers of connected sockets and listening ports!