Ethernet Shield connect to more than 4 servers

Hi,

i’ve got a Problem to connect an Arduino UNO with a Ethernet Shield W5100 to more than 4 Servers over the Etherner Library.
When i try to connect to 4 Servers it works… but when i try more than 4 it stucks and can’t get connected.

I read the distance of a sensor and send it to 6 Server (5 for Animation, 1 for Light)
I’ve changed the order of connections in init2() and everytime the last two don’t connect.
Then i only try 5, same failure - last one don’t connect.
The i try only 4 connections and it works.

Here is my Code i don’t know where the fault is.
I’ve tested the init2() and initConnectionMethod() but same fault.

Can someone please help me to find the mistake?

#include <Ethernet.h>
#include <SPI.h>


// ultrasonic pin
int echoPin = 7; // Input Pin,    digital pin 7
int trigPin= 8; // Output Pin,    digital pin 8

//networkconfig
//Arduino IP 192.168.178.10 - .19
byte mac[] = { 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1 };
byte ip[] = { 192, 168, 178, 10 };
byte subnetmask[] = { 255, 255, 255, 0};

//Light Animation Server IP  192.168.0.1
byte server[] = { 192, 168, 178, 1 }; 
int serverPort = 5555;

//Rasperry Animation IP .20 - .24
int raspPort = 6667;
byte raspA[] = { 192, 168, 178, 20 }; 
byte raspB[] = { 192, 168, 178, 21 };
byte raspC[] = { 192, 168, 178, 22 };
byte raspD[] = { 192, 168, 178, 23 };
byte raspE[] = { 192, 168, 178, 24 };

byte current = -1;
byte zero=0;
byte one=1;
byte two=2;
byte three=3;
byte id=16;

EthernetClient client;
EthernetClient clientRaspA;
EthernetClient clientRaspB; 
EthernetClient clientRaspC;
EthernetClient clientRaspD;
EthernetClient clientRaspE;

void setup() 
{
  Serial.begin (9600); 
  
  Ethernet.begin(mac, ip);

  delay(1000);

 //stay in loop while not connected
 init2(); 
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}

void loop() 
{
    

    if(client.connected() && clientRaspA.connected() && clientRaspC.connected() && clientRaspD.connected() && clientRaspE.connected())
    {
      Serial.println("connected");
      client.write(id);
      sendDataToRasps(id);
    }
    else
    {
          // No connection
    }
      
    while(client.connected() && clientRaspA.connected() && clientRaspC.connected() && clientRaspD.connected() && clientRaspE.connected())
    {
          getSensorData();
    }
}

void init2()
{
   int test=0;
   while((test= client.connect(server, serverPort))!=1)
   {    
   }
   test=0;
   Serial.println("connected to dmx");

   while((test= clientRaspA.connect(raspA, raspPort))!=1)
   {    
   }
   test=0;
   Serial.println("connected to Rasp A");
   
   /*
   while((test=clientRaspB.connect(raspB, raspPort))!=1)
   {    
   }
   test=0;
   Serial.println("connected to Rasp B");
   */
   
   while((test= clientRaspC.connect(raspC, raspPort))!=1)
   {    
   }
   test=0;
   Serial.println("connected to Rasp C");

   
   while((test= clientRaspD.connect(raspD, raspPort))!=1)
   {    
   }
   test=0;
   Serial.println("connected to Rasp D");

   while((test= clientRaspE.connect(raspE, raspPort))!=1)
   {    
   }
   test=0;
   Serial.println("connected to Rasp E");
}

void initConnection()
{
  Serial.println("init connect");
  do
    {   
        int i=client.connect(server, serverPort);
        Serial.println(i);
        delay(30);
    }while(!client.connected());
   Serial.println("connected to dmx");
   
   do
    {
        clientRaspA.connect(raspA, raspPort);
        delay(30);
    }while(!clientRaspA.connected());
    Serial.println("connected to rasp A"); 
    
    do
    {
        clientRaspC.connect(raspC, raspPort);
        delay(30);
    }while(!clientRaspC.connected());
    Serial.println("connected to rasp C");
          
    do
    {
        clientRaspD.connect(raspD, raspPort);
        delay(30);
    }while(!clientRaspD.connected());
    Serial.println("connected to rasp D");

    do
    {
        clientRaspE.connect(raspE, raspPort);
        delay(30);
    }while(!clientRaspE.connected());
    Serial.println("connected to rasp E");
    

    
}

void getSensorData()
{

    long duration; 
    long distance;
    digitalWrite(trigPin, LOW);  
    delayMicroseconds(2); //
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trigPin, LOW);
  
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;  // distance in cm
   
     
      if(distance <0 ) // 
      {
        sendDataToRasps(zero);
      }
      else if (distance >=0 && distance < 20) // >
      { 
        sendDataToRasps(three);
        client.write(one);
      }
      else if(distance >=20 && distance < 60)  
      {
        sendDataToRasps(two);
        client.write(zero);
      }
      else if (distance >= 60 && distance < 100)
      {
        sendDataToRasps(one);
        client.write(zero);
      }
      else //if (distance >=100) 
      {
        sendDataToRasps(zero);
        client.write(zero);
      }
      delay(30);
    
}
void sendDataToRasps(byte value)
{
    clientRaspA.write(value);
    //clientRaspB.write(value);
    clientRaspC.write(value);
    clientRaspD.write(value);
    clientRaspE.write(value);

}

Thank you for your help.

When i try to connect to 4 Servers it works.. but when i try more than 4 it stucks and can't get connected.

There are only 4 sockets, so 4 connections to servers. Unless you (properly) close a connection, that is.

Sending data that way doesn't seem the most efficient. Send the data to a PC as server, and have it send the data to the Rasps.

Thank you for your reply.
Have modified the code on the server and now it works.
I had not read that it only supports 4 sockets, so that was the problem.
Thank you.