Causes of periodic failure in parsing data passed in HTTP GET requests?

I’m doing and HTTP GET that sends /?roundLevel=x to my Arduino with Ethernet Shield. Please note, the x value is hard-coded in there, so that I can do very simple if(readString.indexOf("roundLevel=0")>0) tests. Arduino is running a web server sketch. I have if statements set up in the sketch to execute some code when x = 0, 2 or 3. It’s unreliable. When x is 0 or 2, the arduino usually ‘sees’ that and executes the code in the if statement. It NEVER suceeds when x = 3. I am not sure whether the problem is that 1) It can’t read 3, or 2)this is a timing issue, or 3) it simply fails to read what the GET is sending in after a certain number of requests. Typically (although not always), x=3 on the 7th GET. Is there some reason it might fail after the 7th GET request? All requests are from the same client and can happen as rapidly as within 3 seconds of one another.
I can’t seem to debug this issue using Serial.print() because (for reasons I don’t understand) Serial.begin(9600) interferes with the Arduino-as-server’s ability to accept client requests. Any suggestions? Code below.

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

//Ethernet shield's MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xF7, 0x99 };
//IP address assigned by the network
IPAddress ip(10,132,7,115);
// the router's gateway address
byte gateway[] = { 10, 132, 7, 254 };
//// the subnet:
byte subnet[] = { 255, 255, 254, 0 };

EthernetServer server(80);

String roundOpenTag = "<game>";
String roundCloseTag = "</game>"; 
long randNumber;
String ledColor = "";
int seqArray[5] = {3,5,6,7,8};
int loops = 5;
String readString = String(100);

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
    {
      boolean currentLineIsBlank = true;
      while (client.connected()) 
        {
          if (client.available()) 
            {
              //If there is a request,
              //read the request
              char c = client.read();
        
              //Store characters to String
               readString +=c;
        
              if(readString.indexOf("roundLevel=0")>0)
              {
               roundOpenTag = "<one>"; 
               roundCloseTag = "</one>";
              }
              if(readString.indexOf("roundLevel=2")>0)
              {
               roundOpenTag = "<two>"; 
               roundCloseTag = "</two>";
               loops = 6;
              }
              if(readString.indexOf("roundLevel=3")>0)
              {
               roundOpenTag = "<three>"; 
               roundCloseTag = "</three>";
               loops = 7;
              }
        
         // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
              if (c == '\n' && currentLineIsBlank) 
              {
              // send a standard http response header
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html");
                client.println();
                //this delay gives the player a second to look from phone
                //to Blinky
                delay(1000);
                for (int i = 0; i < loops; i++)
                {
                  //pull randomn number between 0 and 4
                  randomSeed(analogRead(1));
                  randNumber = random(0, 5);
                  
                  if (randNumber == 0)
                  {
                     ledColor = "red"; 
                  }
                  else if (randNumber == 1)
                  {
                      ledColor = "orange";
                  }
                   else if (randNumber == 2)
                  {
                      ledColor = "yellow";
                  }
                   else if (randNumber == 3)
                  {
                      ledColor = "green";
                  }
                   else
                  {
                      ledColor = "blue";
                  }
                  digitalWrite(seqArray[randNumber], HIGH);
                  // set the LED on for 1/4 second
                  delay(250);
                  
                  //write xml open tag
                  client.print(roundOpenTag);
                  //write xml text (light color)
                  client.print(ledColor + "
");
                  
                  digitalWrite(seqArray[randNumber], LOW);
                  //set the LED off for 1/2 second
                  delay(250); 
                  
                  //write xml closed tag
                  client.print(roundCloseTag);
                  }
                  break;
              }
              if (c == '\n') 
              {
                // you're starting a new line
                currentLineIsBlank = true;
              } 
              else if (c != '\r') 
              {
                // you've gotten a character on the current line
                currentLineIsBlank = false;
              }
            }
        }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}
String readString = String(100);

What do you think this is doing?

                  randomSeed(analogRead(1));

should be done once in setup(), not every time you need a random number.

I can’t seem to debug this issue using Serial.print() because (for reasons I don’t understand) Serial.begin(9600) interferes with the Arduino-as-server’s ability to accept client requests. Any suggestions? Code below.

Not for me, it doesn’t.

Personally, I’d ditch the String class. You are making far too liberal use of this class, unnecessarily. Creating char pointers pointing to “red”, “green”, etc, and a char pointer, ledColor, that points to the appropriate color name, rather than dynamically assigning colors would save a bunch of memory. Ditto for the open and close tags.

It is easy to store the client request data in an array, rather than a String object, and to use pointers to point to parts of the request, for strcmp() purposes, for instance. Doing so would allow you to completely get rid of the String class and memory fragmenting that is going on.

I'm a novice coder on a deadline and I agree that the manner in which I have coded this sketch is sloppy and it wastes memory. I don't know what that assignment of the string to String(100) does (I assumed it designated the maximum number of characters, kept it to what was set in the example I read, just in case) and would be glad to be enlightened or pointed to a reference.

I am still hoping for a solution. Could the memory fragmenting could be causing my problem?

Not sure wht you are exactly trying to do, but here is some simple server code that might be of interest.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // 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

String readString; 

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

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\"\">ON</a>"); 
          client.println("<a href=\"/?off\"\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

PaulS - Your instincts were right. I made a very foolish error in the program that is sending the request to my Arduino. zoomcat - this code will help me in the future. If you believe this thread will be of no use to anyone in the future, please feel free to ask the moderators to delete it, or just say so below and I believe I have rights to delete(?). Thanks.