Ethernet shield webserver hangs - reset does not solve!

Hi,
I'm using an Ethernet Shield Wiznet w5100 with Arduino UNO.
I've uploaded the webserver example and everything is ok.
The webpage shows the analogs values reloading every 5 seconds, but after some time it randomly hangs; In this state, Ping still works! It does receive replies. I just can't access the webpage anymore and resetting arduino by the reset button does not fix, I must power off and on completely by removing power source to get it working again... But the webserver keeps hanging after a while...

Any thoughts?
Thanks in advance, and please forgive me for my poor english!

Do you have a SD card in the shield’s slot? If so remove it.

What is the last message on the serial monitor when it stops?

You might be running out of sockets, but normally that is fixed by a reset. Add this function to your code and call it after the
Serial.println(“new client”); call. It will show the status of all sockets. If none of the sockets show a status of 0x0, then you have run out of sockets.

#include <utility/w5100.h>

byte socketStat[MAX_SOCK_NUM];

void ShowSockStatus()
{
  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    Serial.print(F("Socket#"));
    Serial.print(i);
    uint8_t s = W5100.readSnSR(i);
    socketStat[i] = s;
    Serial.print(F(":0x"));
    Serial.print(s,16);
    Serial.print(F(" "));
    Serial.print(W5100.readSnPORT(i));
    Serial.print(F(" D:"));
    uint8_t dip[4];
    W5100.readSnDIPR(i, dip);
    for (int j=0; j<4; j++) {
      Serial.print(dip[j],10);
      if (j<3) Serial.print(".");
    }
    Serial.print(F("("));
    Serial.print(W5100.readSnDPORT(i));
    Serial.println(F(")"));
  }
}

A socket status list:
0x0 = available.
0x14 = socket waiting for a connection
0x17 = socket connected to a client if a server, or a server if a client.
0x22 = UDP socket.

You might want to try my code from the playground. There are two examples on this page.
http://playground.arduino.cc/Code/WebServerST

The webpage shows the analogs values reloading every 5 seconds, but after some time it randomly hangs;

If that web page is the meta refresh example, you might try the below test code. Using the fast data option on the page, I’ve had the page successfully upload 500k+ times over a couple days without hanging.

// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString; 

//////////////////////

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c; 
         } 
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>"); 
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>"); 
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY>
");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("

");
            
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
            
            client.print("
analog input1 is: ");
            client.print(analogRead(analogInPin1));
                        
            client.print("
analog input2 is: ");
            client.print(analogRead(analogInPin2));
            
            client.print("
analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                    
            client.print("
analog input4 is: ");
            client.print(analogRead(analogInPin4));
            
            client.print("
analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.println("
</BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
            client.print("

Arduino analog input data frame:
");
            client.print("&nbsp;&nbsp;<a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a>
");
            client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe>
</HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

I have the same problem: With SD card inserted the webserver example runs fine for about one minute and then hangs. Reset does not help. After a reset the server localIP address prints out as 0.0.0.0.
After removing the SD card the sketch runs fine without hanging anymore.
However, I do need the SD card in my app.
Is there a way that I can use both ethernet and SD card reliably in a sketch?

Zoomkat's sketch above should work ok with the SD card inserted. My sketch in the playground works ok also.
http://playground.arduino.cc/Code/WebServerST

This is the big thing in zoomkat's setup. These must be before the Ethernet.begin() call.

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

This is the big thing in my code. These must be before the SD.begin() call.

  // disable w5100 SPI while starting SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

edit: The difference between zoomkat's code and mine is only which SPI device we start first. Zoomkat starts the w5100 first and I start the SD card first. I start the SD card first because in some sketches, I have my ethernet network settings stored on the SD card.

@SurferTim: Thanks!
Now I (think) I understand (please correct me if I'm wrong):

pin 4 is the chip select for the SD card, so it must be deactivated (HIGH level) when initializing the ethernet chip which uses pin 10 as a chip select and vice versa.
To be on the safe side, I could even disable both chip selects and init both libraries in any order:

pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);

SD.begin(4);
Ethernet.begin(mac,...);
or
Ethernet.begin(mac,...);
SD.begin(4);

I guess I can also safely assume that both the SD library and the Ethernet library take care of leaving their own chip select pins disabled when they are finished using them?
So I can call SD card library functions and Ethernet library functions in any order without worrying about the SS pins myself.

pin 4 is the chip select for the SD card, so it must be deactivated (HIGH level) when initializing the ethernet chip which uses pin 10 as a chip select and vice versa.
To be on the safe side, I could even disable both chip selects and init both libraries in any order:

Yes.

I guess I can also safely assume that both the SD library and the Ethernet library take care of leaving their own chip select pins disabled when they are finished using them?
So I can call SD card library functions and Ethernet library functions in any order without worrying about the SS pins myself.

Yes.