Arduino Ethernet Shield connection to socket server

I’m using a ethernet shield for Arduino to connect it to a socket server (different computer) so that i can receive messages from it to activate some routine. Here is my code:

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


byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x21 };
byte ip[] = { 192,168,1,11 }; //ip shield
byte server[] = { 192,168,1,7 }; // ip server

EthernetClient client;
String readString;

int ledPins[] = {19, 17, 2,3, 5, 6, 7, 8, 9};  // leds pins
int pinCount = 8;// number of leds

int const PINEYES = 9; //pin for different led

int const TIMERLEDS = 1000; 
int const TIMERTOOFF= 3000; 


//--------------------------------------------------------------------------

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

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 1400)) {
    Serial.println("connected");
    client.println();
  } 
  else {
    Serial.println("connection failed");
  }

  pinMode(PINEYES, OUTPUT);      

  int thisPin;

  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
}


//--------------------------------------------------------------------------

void loop()
{
  if (client.available()) {
    char c = client.read();

    if (readString.length() < 30) {
      if(c!='|')
        readString.concat(c);
      else
      {  
        client.flush();
        //if (readString == "START_SENSATIONS") {
        if (readString == "on") {
          Serial.println("recebi");
          client.stop();
          turnOnMaya();
        }
        resetString();
      }
    }
    Serial.println(readString);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

//--------------------------------------------------------------------------

void turnOnMaya(){
  turnOnLeds();

  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
     delay(TIMERLEDS);                  
     digitalWrite(ledPins[thisPin], LOW);    
  }
  turnOnEyes();
  delay(TIMERTOOFF);    
  turnOffLeds();
  digitalWrite(PINEYES, LOW);   
  client.connect(server, 1400);
}

//--------------------------------------------------------------------------

void turnOnLeds(){

  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    digitalWrite(ledPins[thisPin], HIGH);   
  }
}

//--------------------------------------------------------------------------

void turnOffLeds(){

  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    digitalWrite(ledPins[thisPin], LOW);   
  }
}

//--------------------------------------------------------------------------

void turnOnEyes(){
  digitalWrite(PINEYES, 255);
}

//--------------------------------------------------------------------------

void resetString() {
  readString = "";
}

The problem is, when my server stops or is not available for some moments, i need that my arduino keeps on trying to connect to it until it is available again. But i cant make this work. I tried this:

  while(!client.available()){
    Serial.println("connection failed, trying again...");
    client.connect(server, 1400);
    delay(1000);
  }

But it doesnt work. It just prints “connection failed, trying again…” forever. How can i do this? Thanks!!

  while(!client.available()){
    Serial.println("connection failed, trying again...");
    client.connect(server, 1400);
    delay(1000);
  }

Connecting to a server that you are already connected to doesn't make sense. Why are you doing that?

Connecting, again, to a server that you are not .connected() to might makes sense.

PaulS is right. You need something like this:

  Serial.println("connecting...");

  while (client.connect(server, 1400) != 1) {
     Serial.print("connection failed...");
     delay(1000);
     Serial.println("trying again");      
  } 

  Serial.println("connected");

Thanks, that worked! :smiley: