Ethernet Shield (W5100) Stops responding.

So I have been dealing with this for the past few days. I just got my arduino uno R3 and a ebay ethernet shield W5100. Everything works for about 30min to 1hr then things go wrong.

I have it set up with the DHCP Server example but modified so that I can connect to it via putty telnet, I can send messages to it which can turn on or off relays.

After 30min - 1hr I can no longer connect to it. The uno is still working and can process code and I can ping the shield and all the lights on the shield are functioning just like before. It is set up with a static IP and I confirmed this looking at the attached devices on my network. The only way that I know of getting it to work again is to reset it.

I really don't know what else to do. I have been on google for the past few days and I have tried many things and it still freezes up. I don't know if its a bad shield or I just have not tried the correct code.

Any help is appreciated.

Pics when I cannot connect to it.

Edit: Sorry I had some old code. I have updated to use my current code. Not much changed tho.

Code:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

int relays[] = {2, 3, 4, 5, 6, 7, 8, 9}; //Pins that relays are on in order from 1 - 8
String relayName[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"}; //Relay Names in order from 1 - 8
int randomNumber = 0;
int input = 0; 
int val = 0;
int welcomeFlag = 0;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; // Enter a MAC address and IP address for your controller.
IPAddress ip(192, 168, 1, 177); // The IP address will be dependent on your local network.
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 1); // gateway and subnet are optional:
IPAddress subnet(255, 255, 255, 0); // gateway and subnet are optional:
EthernetServer server(23); // telnet defaults to port 23

void setup() {
  Serial.begin(9600); // Open serial communications and wait for port to open:
  Ethernet.begin(mac, ip, myDns, gateway, subnet); //Starts Ethernet Connection
  delay(1000);
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) { // print your local IP address:
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  server.begin(); // start listening for clients
  //Pin Setups and modes;
  for(int z = 0; z < 8; z++){
    pinMode(relays[z], OUTPUT);
    digitalWrite(relays[z], HIGH);
  }
}

void loop() {
  EthernetClient client = server.available(); // wait for a new client:
  while (client.connected()) {
    if(welcomeFlag == 0){
      client.println("Connected...");
      welcomeFlag = 1;
    }
    delay(100);
    //client.print("hello");
    input = client.read(); // read the bytes incoming from the client:
    if(input == 104){
      helpme(client); //Calls helpme fuction
    }
    else if(input == 113){
      Serial.println("Disconnection");
      client.print("Disconnected");
      delay(1000);
      client.stop();
    }
    else if(input == 100){
      for (int r = 0; r < 100; r++){
        randomNumber = random(10);
        relay(randomNumber, client);
        delay(100);
      }
      turnAllOff(client);
    }
    else{
      input -= 49;
      if(input >= 0 && input <= 7){
        relay(input, client); //Calls for a single relay to turn on or off
      }
      else if(input == -1){
        turnAllOff(client); //Calls fuction to turn all relays off
      }
      else if(input == 8){
        turnAllOn(client); //Calls fuction to turn all relays on
      }
    }
  }
  client.stop();
  if(welcomeFlag == 1){
    welcomeFlag = 0;
  }
}

void relay(int x, EthernetClient client){
  client.println("--------------");
  val = digitalRead(relays[x]);
  if(val == 1){
    digitalWrite(relays[x], LOW);
    client.print("turning Relay ");
    client.print(relays[x] - 1);
    client.println(" on");
  }
  else{
    digitalWrite(relays[x], HIGH);
    client.print("turning Relay ");
    client.print(relays[x] - 1);
    client.println(" off");
  }
}

void turnAllOff(EthernetClient client){
  client.println("--------------");
  for(int i = 0; i < 8; i++){
    digitalWrite(relays[i], HIGH);
    client.print("turning Relay ");
    client.print(relays[i] - 1);
    client.println(" off");
  }
}

void turnAllOn(EthernetClient client){
  client.println("--------------");
  for(int i = 0; i < 8; i++){
    digitalWrite(relays[i], LOW);
    client.print("turning Relay ");
    client.print(relays[i] - 1);
    client.println(" on");
  }
}

void helpme(EthernetClient client){
  client.println("--------------");
  int oneornone = 0;
  for(int y = 0; y < 8; y++){
    client.print("relay ");
    client.print(y + 1);
    client.print(" - ");
    client.print(relayName[y]);
    oneornone = digitalRead(relays[y]);
    if(oneornone == 1){
      client.print("\t\t");
      client.println("(-off-)");
    }
    else{
      client.print("\t\t");
      client.println("(on)");
    }
  }
}

What's the serial output in that case?
I guess it either gets stuck in the while(client.connected()) loop (insert a timeout there) or the usage of the String class results in a memory lock up. To save some memory you should learn to use the F() macro.

I did put in the F() macros as I just learned that a few hours ago. As well Ill slap a timeout in my code as well. Do I just comment out the string array as well as the code that uses it?
Here is my updated code. Ill report back if its fixed or still screws up. Thanks for the help.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

int relays[] = {2, 3, 4, 5, 6, 7, 8, 9}; //Pins that relays are on in order from 1 - 8
//String relayName[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"}; //Relay Names in order from 1 - 8
int randomNumber = 0;
int input = 0; 
int val = 0;
int welcomeFlag = 0;
unsigned long timeout = 60000;
unsigned long previousMillis = 0;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; // Enter a MAC address and IP address for your controller.
IPAddress ip(192, 168, 1, 177); // The IP address will be dependent on your local network.
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 1); // gateway and subnet are optional:
IPAddress subnet(255, 255, 255, 0); // gateway and subnet are optional:
EthernetServer server(23); // telnet defaults to port 23

void setup() {
  Serial.begin(9600); // Open serial communications and wait for port to open:
  Ethernet.begin(mac, ip, myDns, gateway, subnet); //Starts Ethernet Connection
  delay(1000);
  //Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) { // print your local IP address:
    // print the value of each byte of the IP address:
    //Serial.print(ip[thisByte], DEC);
    //Serial.print(".");
  }
  //Serial.println();
  server.begin(); // start listening for clients
  //Pin Setups and modes;
  for(int z = 0; z < 8; z++){
    pinMode(relays[z], OUTPUT);
    digitalWrite(relays[z], HIGH);
  }
  digitalWrite(10,HIGH);
}

void loop() {
  EthernetClient client = server.available(); // wait for a new client:
  delay(100);
  while (client.connected()) {
    //timeout
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= timeout){
      client.println(F("timeout"));
      client.stop();
    }
    //timeout
    //Serial.println("hello");
    //Serial.println(freeRam());
    if(welcomeFlag == 0){
      client.println(F("Connected..."));
      //Serial.println("User Connected...");
      welcomeFlag = 1;
    }
    delay(100);
    input = client.read(); // read the bytes incoming from the client:
    if(input == 104){
      previousMillis = currentMillis;
      helpme(client); //Calls helpme fuction
    }
    else if(input == 113){
      //Serial.println("Disconnection");
      client.print(F("Disconnected"));
      delay(1000);
      client.stop();
    }
    else if(input == 100){
      previousMillis = currentMillis;
      for (int r = 0; r < 100; r++){
        randomNumber = random(10);
        relay(randomNumber, client);
        delay(100);
      }
      turnAllOff(client);
    }
    else{
      input -= 49;
      if(input >= 0 && input <= 7){
        previousMillis = currentMillis;
        relay(input, client); //Calls for a single relay to turn on or off
      }
      else if(input == -1){
        previousMillis = currentMillis;
        turnAllOff(client); //Calls fuction to turn all relays off
      }
      else if(input == 8){
        previousMillis = currentMillis;
        turnAllOn(client); //Calls fuction to turn all relays on
      }
    }
  }
  client.stop();
  if(welcomeFlag == 1){
    welcomeFlag = 0;
  }
  //Serial.println(freeRam());
}

void relay(int x, EthernetClient client){
  client.println(F("--------------"));
  val = digitalRead(relays[x]);
  if(val == 1){
    digitalWrite(relays[x], LOW);
    client.print(F("turning Relay "));
    client.print(relays[x] - 1);
    client.println(F(" on"));
  }
  else{
    digitalWrite(relays[x], HIGH);
    client.print(F("turning Relay "));
    client.print(relays[x] - 1);
    client.println(F(" off"));
  }
}

void turnAllOff(EthernetClient client){
  client.println(F("--------------"));
  for(int i = 0; i < 8; i++){
    digitalWrite(relays[i], HIGH);
    client.print(F("turning Relay "));
    client.print(relays[i] - 1);
    client.println(F(" off"));
  }
}

void turnAllOn(EthernetClient client){
  client.println(F("--------------"));
  for(int i = 0; i < 8; i++){
    digitalWrite(relays[i], LOW);
    client.print(F("turning Relay "));
    client.print(relays[i] - 1);
    client.println(F(" on"));
  }
}

void helpme(EthernetClient client){
  client.println(F("--------------"));
  int oneornone = 0;
  for(int y = 0; y < 8; y++){
    client.print(F("relay "));
    client.print(y + 1);
    client.print(F(" - "));
    //client.print(relayName[y]);
    oneornone = digitalRead(relays[y]);
    if(oneornone == 1){
      client.print(F("\t\t"));
      client.println(F("(-off-)"));
    }
    else{
      client.print(F("\t\t"));
      client.println(F("(on)"));
    }
  }
}

int freeRam()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}