Program works on ESP8266 but only partially on ESP32

I’m slowly learning the ins and outs of networking with ESP8266 ESP-01 and ESP32. I’m trying to run an ESP32 as a server that reads from two ESP8266 ESP-01 clients. Everything works when I have an ESP-01 as the server. However, when I use an ESP32 instead, it only reads the data the first few attempts from each client. After that it is unable to communicate with the clients. Here is my code when the ESP-01 is the server. The only change I make with the ESP32 is changing the library to #include <WiFi.h>. I wonder if I’m selecting the wrong board when loading the code from the Arduino IDE. Any thoughts?

Board I purchased.
The underside says ESP32 DEVKITV1

I’ved tried NodeMCU-32S as the board and DOIT ESP32 DEVKIT V1.

#include <SPI.h>
#include <ESP8266WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "*********"; //SSID here
char pass[] = "*********"; // Passowrd here
//-----------------------------//

#define MAX_CLIENTS 2

WiFiServer server(80);
WiFiClient *clients[MAX_CLIENTS] = { NULL };

IPAddress ip(192, 168, 1, 137);            // IP address of the server
IPAddress gateway(192,168,1,1);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network

String request = "";

void setup() {
  Serial.begin(115200);
  internetServer();
}

void loop() {
  WiFiClient newClient = server.available();
  if (newClient) {
    Serial.println("New client.");
    if (newClient.connected()) {
      Serial.println("Sever connected.");
      for (int i=0 ; i<MAX_CLIENTS ; ++i) {
        if (NULL == clients[i]) {
            clients[i] = new WiFiClient(newClient);
            break;
        }
      }
    }
    for (int i=0 ; i<MAX_CLIENTS ; ++i) {
      if (NULL != clients[i] && clients[i]->available() ) {
        clients[i] = new WiFiClient(newClient);
        request = newClient.readStringUntil('\r');
        Serial.print("From client: ");
        Serial.println(request);
        newClient.println("Data received!\r"); //
      }
    }
    newClient.flush();
    newClient.stop();
    request = "";
  }
}

void internetServer() {
  if (WiFi.status() != WL_CONNECTED) {
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      WiFi.config(ip, gateway, subnet);
      WiFi.begin(ssid, pass);
      delay(5000);
    }
    server.begin();                        
    Serial.println("Connected to wifi");
    Serial.print("Status: "); Serial.println(WiFi.status()); 
    Serial.print("IP: ");     Serial.println(WiFi.localIP());
    Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
    Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
    Serial.print("SSID: "); Serial.println(WiFi.SSID());
    Serial.print("Signal: "); Serial.println(WiFi.RSSI());
    Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
  }
}

I had made some changes that seemed to allow a few more cycles of communication before things would crash. I added some buttons, LEDs, and toggle code and I can tell that the ESP32 is freezing and that’s why the communication is stopped. I’m able to toggle the LEDs up until I stop getting receipt confirmations on the client side.

I have been largely following this example, but I really don’t know what they’re talking about with the inputs. I’m thinking this is probably my issue. Would anyone mind helping me understand that portion or what I should research?

For simplicity I left the LEDs and toggle stuff out of the post.

#include <SPI.h>
#include <WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "********"; //SSID here
char pass[] = "********"; // Passowrd here
//-----------------------------//

#define MAX_CLIENTS 2

WiFiServer server(80);
WiFiClient *clients[MAX_CLIENTS] = { NULL };

IPAddress ip(192, 168, 1, 137);            // IP address of the server
IPAddress gateway(192,168,1,1);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network

String request = "";
unsigned long dataRecvd = 0;

void setup() {
  Serial.begin(115200);
  internetServer();
}

void loop() {
  if (millis() - dataRecvd > 20100) {
    WiFi.disconnect();
    internetServer();
  }
  WiFiClient newClient = server.available();
  if (newClient) {
    Serial.println("New client.");
    if (newClient.connected()) {
      Serial.println("Sever connected.");
      for (int i=0 ; i<MAX_CLIENTS ; ++i) {
        if (NULL == clients[i]) {
            clients[i] = new WiFiClient(newClient);
            break;
        }
      }
    }
    for (int i=0 ; i<MAX_CLIENTS ; ++i) {
      if (NULL != clients[i] && clients[i]->available() ) {
        request = clients[i]->readStringUntil('\r');    // receives the message from the client
        Serial.print("From client: ");
        dataRecvd = millis();
        Serial.println(request);
        clients[i]->println("Data received!\r"); //
        clients[i]->flush();
        clients[i]->stop();
        delete clients[i];
        clients[i] = NULL;
      }
    }
    request = "";
  }
}
 
void internetServer() {
  dataRecvd = millis();
  if (WiFi.status() != WL_CONNECTED) {
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      WiFi.config(ip, gateway, subnet);
      WiFi.begin(ssid, pass);
      delay(5000);
    }
    server.begin();
    Serial.println("Connected to wifi");
    Serial.print("Status: "); Serial.println(WiFi.status());
    Serial.print("IP: ");     Serial.println(WiFi.localIP());
    Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
    Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
    Serial.print("SSID: "); Serial.println(WiFi.SSID());
    Serial.print("Signal: "); Serial.println(WiFi.RSSI());
    Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
  }
}

Adding a delay to the void loop helped. I changed that to a 500ms timer and it crashed after just shy of an hour when normally it would crash within 1-2 mins. I'm going to try the delay again to see if that runs longer...

I had never looked at the debug level tool in the Arduino IDE before. I never gave it enough time to crash again to see what error showed then, but I was seeing an error when it was flushing the clients. It was saying there were "no more processes." I assume that is because the clients were also doing their own flushing on their end. I tweaked that part of the code on the server end and so far it's been going a couple hours with no issue. I think it may be solved...