Ethernet buffer OVERFLOW??

Hello, I’m having a problem and may be someone from here can help me. I’m with an Arduino Mega 2560 + Ethernet Shield W5100. The code is:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 10);
EthernetServer server(80);

boolean estado=true;

String HTTP_req;

void setup()
{
   Ethernet.begin(mac, ip);
   server.begin();
   //Serial.begin(115200);       // for diagnostics
}

void loop()
{
   EthernetClient client = server.available();  // try to get client

   if (client) {
       boolean currentLineIsBlank = true;
       while (client.connected()) {
           if (client.available()) {
               char c = client.read();
               HTTP_req += c;
               if (c == '\n' && currentLineIsBlank) {
                   client.println("HTTP/1.1 200 OK");
                   client.println("Content-Type: text/html");
                   client.println("Connection: keep-alive");
                   client.println();
                   if (HTTP_req.indexOf("ajax_switch") > -1) {
                       GetAjaxData(client);
                   }
                   else {
                       client.println("<!DOCTYPE html>");
                       client.println("<html>");
                       client.println("<head>");
                       client.println("<title>Arduino Web Page</title>");
                       client.println("<script>");
                       client.println("function GetSwitchAnalogData() {");
                       client.println("nocache = \"&nocache=\" + Math.random() * 1000000;");
                       client.println("var request = new XMLHttpRequest();");
                       client.println("request.onreadystatechange = function() {");
                       client.println("if (this.readyState == 4) {");
                       client.println("if (this.status == 200) {");
                       client.println("if (this.responseText != null) {");
                       client.println("document.getElementById(\"sw_an_data\").innerHTML = this.responseText;");
                       client.println("}}}}");
                       client.println("request.open(\"GET\", \"ajax_switch\" + nocache, true);");
                       client.println("request.send(null);");
                       client.println("setTimeout('GetSwitchAnalogData()', 1000);");
                       client.println("}");
                       client.println("</script>");
                       client.println("</head>");
                       client.println("<body onload=\"GetSwitchAnalogData()\">");
                       client.println("<h1>Arduino AJAX Input</h1>");
                       client.println("<div id=\"sw_an_data\">");
                       client.println("</div>");
                       client.println("</body>");
                       client.println("</html>");
                   }
                   //Serial.print(HTTP_req);
                   HTTP_req = "";
                   break;
               }

               if (c == '\n') {
                   currentLineIsBlank = true;
               } 
               else if (c != '\r') {
                   currentLineIsBlank = false;
               }
           } // end if (client.available())
       } // end while (client.connected())
       delay(1);
       client.stop();
   } // end if (client)
}

void GetAjaxData(EthernetClient cl)
{
   cl.print("<p>Estado: ");
   cl.print(estado);
   cl.println("</p>");
   estado=!estado; //to realize when the changes are made
}
//---------------------------------------------------------------------------

The problem is that after a period of time, the ajax update stops. If I enable the serial for debugging, I receive the HTTP request, I send the header and body, and after this I star to receive the ajax request every second. Everything looks good until, for some reason that I couldn’t find yet, the input buffer (I think that is the responsable) starts to fail. With the debugging I got as HTTP request:

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

GET /ajax_switch&nocache=833922.3657859031 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.10/
Connection: keep-alive

GET /ajax_switch&nocache=722496.1500752304 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.10/
Connection: keep-alive

GET /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: httpx_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
ReferGET /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: hajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
RefereET /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
//---------------------------------------------------------------------------

So in the las request I never find the “end request character”. Another strange thing is that if I reupload the code to the Arduino with disconnecting the USB, I got the same problem but from the beginning; The first request I get:

//---------------------------------------------------------------------------
GET / HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-EncodinGET / HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gz HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-EncodingET / HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gziHTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding:T / HTTP/1.1
Host: 192.168.0.10
//----------------------------------------------------------------------------

So I never receive the “end character” and I never complete the first request. But if I unplug and plug again the USB, everything works like the first time. It seems that there is some buffer inside the ethernet shield that keep the info until in unpower.
Can anyone help me?

[Yes -don’t post multiple copies of the same message across multiple forums. Post once, have a little patience.
AND PLEASE USE CODE TAGS! IT’s the </> button in the menu]

Do you have the special Mega model with 4 terabytes of memory? I doubt it. You are pissing away SRAM like you have an unlimited amount of it. You do NOT.

Ditch the String class COMPLETELY.

Learn to use the F() macro.
client.println(F(""));