Ethernet shield + webserver hangs?

hi, after a few hits (max 7 hits )on my webserver it hangs?, I type either 192.168.0.137/?0 or 192.168.0.137/?1
Anyone with good ideas? or is there something in the ethernet library that is making my program hangs?

/*
 * Web Server
 *
 * A simple web server that control and show status of relay
 */
//Arduino uses digital pins 10, 11, 12, and 13 (SPI) to communicate 
//with the W5100 on the ethernet shield. These pins cannot be used for general i/o. 

#include <Ethernet.h>
#include <String.h>

byte mac[]     = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[]      = { 192, 168, 0, 137 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[]  = { 255, 255, 255, 0 };

const int relaypin = 6; //The relay is attached to this pin.
boolean relaystatus_on = false; //Define if relay is switched ON=true, or OFF=false
char cbuf[30]; //String that contains received http response
int i=0;       //Count up to 30 char for http response
        
Server server(80);


void setup()
{
  // Initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  //Ethernet.begin(mac, ip);

  // Start web server
  server.begin();

  // Initialize the relaypin as an output:
  pinMode(relaypin, OUTPUT);
}


void loop()
{
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        // Read only the first chars in wep response
        if (i<30){
          cbuf[i]=c;
          i++;
        }
               
        // if we've gotten to the end of the line (received a newline character)
        // and the line is blank, the http request has ended, so we can send a reply
        if (c == '\n' && current_line_is_blank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("
");
          break;
        }
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } 
        else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }    

    //Read enough of wep response
    if (i==30){
       cbuf[i]='\0'; //terminating the string with \0
       i=0; // Reset so we only read the http respones one and first time.
    }

    // Switch relay ON
    if ((((int)strstr(cbuf, "?1"))!=0) && (relaystatus_on==false)) {
       digitalWrite(relaypin, HIGH);    // Switch ON relay.
       relaystatus_on=true;
       client.println("Relay is switched ON");
    }
  
    // Switch relay OFF
    else if ((((int)strstr(cbuf, "?0"))!=0) && (relaystatus_on==true)) {
       digitalWrite(relaypin, LOW);    // Switch OFF relay.
       relaystatus_on=false;
       client.println("Relay is switched OFF");
    }
  
    //If wrong commands or no commands write below  
    else {
       if (relaystatus_on==true) {client.println("Relay status is ON"); }
       if (relaystatus_on==false){client.println("Relay status is OFF"); }
    }
    client.println("

Switch relay OFF: 192.168.0.137/?0 
Switch relay ON : 192.168.0.137/?1");

    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
//Http response is done to browser and time to continue program.
  


} //EOF

I forgot to tell I'm using Arduino 0018 Software

Well I got a workaround.

I added a little timing delay - I like the number 7, nice primnumber..Try other yourself.

The code snippet, changes to previous code is bold

// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
** delay(7); //timing delay else webserver hangs.**
if (client.available()) {
char c = client.read();

Just wondering - is there some some timingstuff on the SPI bus!?! that the Arduino/Ethernet library is violating??

//Arduino uses digital pins 10, 11, 12, and 13 (SPI) to communicate
//with the W5100 on the ethernet shield. These pins cannot be used for general i/o.

Hi,
I had the same problem with a web server running on the arduino. I was receiving the characters of the http header and testing if a certain sequence had arrived. When the program detected the sequence, it executed the action but after a small amount of tries it hanged. My solution was to read the entire http header, flag the detection of the sequence but only execute the action after reading all the header. The http header, depending on the browser or device connected, can have more than 600 characters. After this correction ,it has not hanged anymore and stayed responsive for a whole month.
Jeronimo
www.blogdoje.com.br
AVR,Arduino & ARM

Thank you

My solution was to read the entire http header, flag the detection of the sequence but only execute the action after reading all the header.

Can you post the section of code for collecting the whole header? thanks!

Something else to my original question - I have done the same as telnet session. And it was also hanging after some toggles of the relay. Always after I have switched the relay ON - It must be the current consumption of the transistor that I use for driving the relay.

Because When I add the //
// digitalWrite(relaypin, HIGH); // Switch ON relay.

Then I 'simulated' to click the relay on (I put the same // to switch relay off) Then my Telnet session NEVER hang...

Damn HW solution I have done :-[ But I was sure that I used less than 40mA !?! on pin 6 of the Arduino...

Anyway I will try a resistor to limit the current and post my succes later