How to disconnect client from server to accept new client?

I'm connecting an industrial FANUC robot as a client to an arduino as a server, and I based most of my code off Seeduino Ethernet shield V2.0's chatserver example. My arduino is sending information to the robot constantly in the loop, but I want it to stop sending information once my client disconnects/errors out. However, when I check the serial monitor the print statements continue to print even when I aborted all functions from my robot's side. My robot sends like 20 bytes of data constantly regarding desired temperatures but will stop sending data once I do a manual abort. Once that happens I expected my code to exit the while (client.connected()) loop. How should I proceed with this to make my arduino realize the client.connected() is false and begins listening for a brand new client? (go back to the first line of the loop function/actually loop around).

void loop() {

 EthernetClient client = server.available();

 if (client) {
   while (client.connected()) {
     while (client.available()) {
       Serial.println("");
       char c = client.read();

       switch (c) {
         case 'A': {
           
             int temp1 = client.parseInt();
             ReqEndTemp = double(temp1);
           
             break;
           }
         case 'B': {
           
             int temp2 = client.parseInt();
             ReqBedTemp = double(temp2);
       
             break;
           }
         case '\r':{
             break;
           } 
       }
     }
     delay(1000);
     
  

       CurrentEndTemp = int(Thermistor(analogRead(0)));//Thermistor is a temperature reading function. 
       HEPID.Compute(); 

      
       CurrentBedTemp = int(Thermistor(analogRead(1)));   
       myPID.Compute();
       
      

       
       delay(250);
       Serial.print(" 2 ");
       client.print(" 2 ");
       int tempA = int(CurrentEndTemp);
       Serial.print(tempA); 
       client.print(tempA);
       delay(1000);
       Serial.print(" 3 ");
       client.print(" 3 ");
       int tempB = int(MagE);
       Serial.print(tempB); 
       client.print(tempB);
       delay(1000);
       
       Serial.print(" 4 ");
       client.print(" 4 ");
       int tempC = int(CurrentBedTemp);
       Serial.print(tempC); 
       client.print(tempC);
       delay(1000);
       
       Serial.print(" 5 ");
       client.print(" 5 ");
       int tempD = int(MagB);
       Serial.print(tempD); 
       client.print(tempD);
       delay(1000);
       

       if ((CurrentEndTemp >= ReqEndTemp) && (CurrentBedTemp >= ReqBedTemp) && (CurrentEndTemp >= 100)){
         ROBOT_START = 1;
       } else {
         ROBOT_START = 0;
       }
       Serial.print(" 6 ");
       client.print(" 6 ");
       int tempE = int(ROBOT_START);
       Serial.print(tempE); 
       client.print(tempE);

       Serial.print(ReqEndTemp);
      
       Serial.print(ReqBedTemp);
       delay(1000);
       Serial.println("");
       //analogWrite(3, MagE);
       //analogWrite(4, MagB);
       

     
   }
 }
}

You may have to configure your client for timeout or other indication of lost connection. Else it may be configured to wait indefinitely, by default.

If the client has nothing to send, how should it detect a lost connection? Perhaps something like a ping may help to detect a broken connection.

Ok so I added an extra case C that checks if the client is still connected or not, my robot will just constantly send a char ‘C’ to the arduino as well to tell that it’s there, otherwise get out of the while loop.

void loop() {
  // wait for a new client:

  EthernetClient client = server.available();
  if (client) {
    while (client.connected() && connection) {
      connection = false;
      while (client.available()) {
        char c = client.read();
        switch (c) {
          case 'A': {
              int temp1 = client.parseInt();
              ReqEndTemp = double(temp1);
              break;
            }
          case 'B': {
              int temp2 = client.parseInt();
              ReqBedTemp = double(temp2);
              break;
            }
          case 'C': {
            connection = true;
          }
          case '\r':{
              break;
            } 
        }
      }
      delay(500);
        CurrentEndTemp = int(Thermistor(analogRead(0)));
        //int Ret = client.read();
        HEPID.Compute(); 
  
       
        CurrentBedTemp = int(Thermistor(analogRead(1)));   
        myPID.Compute();
        
       
  
        
        delay(100);
        Serial.print(" 2 ");
        client.print(" 2 ");
        int tempA = int(CurrentEndTemp);
        Serial.print(tempA); 
        client.print(tempA);
        delay(100);
        Serial.print(" 3 ");
        client.print(" 3 ");
        int tempB = int(MagE);
        Serial.print(tempB); 
        client.print(tempB);
        delay(100);
        Serial.print(" 4 ");
        client.print(" 4 ");
        int tempC = int(CurrentBedTemp);
        Serial.print(tempC); 
        client.print(tempC);
        delay(100);
        Serial.print(" 5 ");
        client.print(" 5 ");
        int tempD = int(MagB);
        Serial.print(tempD); 
        client.print(tempD);
        delay(100);
        
  
        //***********************control loop******************************/
        if ((ReqEndTemp-3 < CurrentEndTemp <= ReqEndTemp+3) && (ReqBedTemp-3 < CurrentBedTemp <= ReqBedTemp + 3) && (CurrentEndTemp >= 100)){
          ROBOT_START = 1;
        } else {
          ROBOT_START = 0;
        }
        Serial.print(" 6 ");
        client.print(" 6 ");
        int tempE = int(ROBOT_START);
        Serial.print(tempE); 
        client.print(tempE);
        Serial.print(" ");
        Serial.print(ReqEndTemp);
        Serial.print(" <--Ret Rbt--> ");
        Serial.print(ReqBedTemp);
        delay(1000);
        Serial.println("");
        analogWrite(3, MagE);
        analogWrite(5, MagB);
    }
    client.flush();
    client.stop();

  }
}

However, when I abort/stop my robot, then restart it’s function again to send data, the serial monitor doesn’t seem to accept it as a new connection. I believe client.flush and client.stop are enough to remove the client? I seem to only have this issue when I keep my serial monitor open (it still has the prints from the original client). When I close my serial monitor, then reopen it, it seems like setup runs again (since the print statement from my setup shows up) and it’s a blank state once more. Is there a way to work this in code instead of manually having to open/close my serial monitor each time I want to have a new client? So I don’t keep on redoing my setup again?

EDIT: Resolved my issue at least, what I did was consult this post: http://forum.arduino.cc/index.php/topic,42205.0.html

Figured I just needed to reset my arduino since I’m only looking for 1 client connection, so why not reset entire thing after each completion?