Connection error with Arduino Ethernet shield

with this sketch I can connect into the html site on arduino

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xAC, 0x68 };
File htmlFile;
EthernetServer server(80);
 
void setup()
{
  Serial.begin(9600);
  if(!Ethernet.begin(mac)) Serial.println("dhcp failed");
    else {
       Serial.print("IP is ");
       Serial.println(Ethernet.localIP());
    }
  server.begin();
   if (!SD.begin(4)) {
        Serial.println("Errore nella inizializzazione della SD");
        return;    // init failed
    }
}
 
void loop()
{
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
           
          htmlFile = SD.open("home.htm",FILE_READ);
          if (htmlFile) {
            while (htmlFile.available()) {
                char c = htmlFile.read();
                client.print(c);
                Serial.print(c);
            }
            // close the file:
            htmlFile.close();
          }else Serial.print(" Errore nella lettura file "); 
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }else Serial.print(" Client non accessibile "); 
    }
    delay(1);
    client.stop();
  }
}

but with this i can't.

  #include <SPI.h>
  #include <Ethernet.h>
  #include <SD.h>
   
  byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xAC, 0x68 };
  File htmlFile;
  //creao un oggetto server che rimane in ascolto sulla porta
  //specificata
  EthernetServer server(80);
  int ledPin6 = 6;
  int ledPin7 = 7;
  int ledPin8 = 8;
  int ledPin9 = 9; // LED pin  
  String readString;
   
  void setup()
  {
    pinMode(ledPin6, OUTPUT);
    pinMode(ledPin7, OUTPUT);
    pinMode(ledPin8, OUTPUT);
    pinMode(ledPin9, OUTPUT);
    Serial.begin(9600);
    if(!Ethernet.begin(mac)) Serial.println("dhcp failed");
      else {
         Serial.print("IP is ");
         Serial.println(Ethernet.localIP());
      }
    server.begin();
     if (!SD.begin(4)) {
          Serial.println("Errore nella inizializzazione della SD");
          return;    // init failed
      }
  }
   
  void loop()
  {
    readString = "";
    EthernetClient client = server.available();
    if (client) {
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();
          if (readString.length() < 100) 
          {
            readString += c; //CONCATENA I CARATTERI
          }
          if (c == '\n') {
            if(readString.indexOf("Luci") > 0){
              //variabile per pagina html
              String pagina = "";
              //INIZIO pagina HTML
              pagina = "<html><head><title> Luci Stanze </title></head><body>";
              if(readString.indexOf("Luci1=1") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("Luci_cucina_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin6, HIGH);
              }
              else if (readString.indexOf("Luci2=1") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("Luci_salotto_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin7, HIGH);
              }
              else if (readString.indexOf("Luci3=1") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("Luci_bagno_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin8, HIGH);
              }
              else if (readString.indexOf("Luci4=1") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("Luci_letto_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin9, HIGH);
              }
              else if(readString.indexOf("Luci1=0") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("Luci_cucina_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin6, LOW); 
              }
              else if (readString.indexOf("Luci2=0") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("Luci_salotto_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin7, LOW); 
              }
              else if (readString.indexOf("Luci3=0") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("Luci_bagno_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin8, LOW); 
              }
              else if (readString.indexOf("Luci4=0") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("Luci_letto_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin9, LOW);   
              }
              //FINE pagina HTML
              pagina = pagina + "</body></html>";
              Serial.print(pagina);
              }
            else{
              //HOME PAGE
             
              htmlFile = SD.open("home.htm",FILE_READ);
              if (htmlFile) {
                while (htmlFile.available()) {
                  char c = htmlFile.read();
                  client.print(c);
                  Serial.print(c);
                }
              // close the file:
              htmlFile.close();
            }else Serial.print(" Errore nella lettura file "); 
            break;
          }
          }
        }else Serial.print(" Client non accessibile ");
      }
      delay(1);
      client.stop();
    }else Serial.print(" client ");
  }

why?

home.htm

<html>
 <head><title> Home </title></head>
 <body>
  <p style="text-align: center;"><span style="font-size: 24pt; font-family: times new roman,times;">Sistema di controllo</span></p>
  <p style="text-align: center;"><span style="font-size: 14pt;">Tesina esame di Stato 2013/2014</span></p>
  <p style="text-align: center;">&nbsp;</p>
  <p style="text-align: center;">&nbsp;</p>
  <table style="height: 171px; width: 275px; margin-left: auto; margin-right: auto;" border="0" width="147">
   <tbody>
    <tr>
     <td><a href='Luci'>Luci</a></td>
     <td>&nbsp;</td>
    </tr>
    <tr>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
    </tr>
   </tbody>
  </table>
 </body>
</html>

Luci_bagno_off.htm

<h1>LUCE BAGNO</h1>
  <table border="0">
   <tr>
    <td>Stato: OFF
<a href='Luci3=0'> SPEGNI </a><a href='Luci3=1'> ACCENDI </a></td>
    <td><img src=http://img10.imageshack.us/img10/4719/spenta.jpg />
</td>
   </tr>
  </table>

Luci_bagno_on.htm

  <h1>LUCE BAGNO</h1>
  <table border="0">
   <tr>
    <td>Stato: ON
<a href='Luci3=0'> SPEGNI </a><a href='Luci1=3'> ACCENDI </a></td>
    <td><img src=http://img849.imageshack.us/img849/1513/accesao.jpg />
</td>
   </tr>
  </table>

Luci_cucina_off.htm

  <h1>LUCE CUCINA</h1>
  <table border="0">
   <tr>
    <td>Stato: OFF
<a href='Luci1=0'> SPEGNI </a><a href='Luci1=1'> ACCENDI </a></td>
    <td><img src=http://img10.imageshack.us/img10/4719/spenta.jpg />
</td>
   </tr>
  </table>

Luci_cucina_on.htm

  <h1>LUCE CUCINA</h1>
  <table border="0">
   <tr>
    <td>Stato: ON
<a href='Luci1=0'> SPEGNI </a><a href='Luci1=1'> ACCENDI </a></td>
    <td><img src=http://img849.imageshack.us/img849/1513/accesao.jpg />
</td>
   </tr>
  </table>

Luci_letto_off.htm

  <h1>LUCE CAMERA DA LETTO</h1>
  <table border="0">
   <tr>
    <td>Stato: OFF
<a href='Luci4=0'> SPEGNI </a><a href='Luci4=1'> ACCENDI </a></td>
    <td><img src=http://img10.imageshack.us/img10/4719/spenta.jpg />
</td>
   </tr>
  </table>

Luci_letto_on.htm

  <h1>LUCE CAMERA DA LETTO</h1>
  <table border="0">
   <tr>
    <td>Stato: ON
<a href='Luci4=0'> SPEGNI </a><a href='Luci4=1'> ACCENDI </a></td>
    <td><img src=http://img849.imageshack.us/img849/1513/accesao.jpg />
</td>
   </tr>
  </table>

Luci_salotto_off.htm

  <h1>LUCE SALOTTO</h1>
  <table border="0">
   <tr>
    <td>Stato: OFF
<a href='Luci2=0'> SPEGNI </a><a href='Luci2=1'> ACCENDI </a></td>
    <td><img src=http://img10.imageshack.us/img10/4719/spenta.jpg />
</td>
   </tr>
  </table>

Luci_salotto_on.htm

  <h1>LUCE SALOTTO</h1>
  <table border="0">
   <tr>
    <td>Stato: ON
<a href='Luci2=0'> SPEGNI </a><a href='Luci2=1'> ACCENDI </a></td>
    <td><img src=http://img849.imageshack.us/img849/1513/accesao.jpg />
</td>
   </tr>
  </table>

Are you able to open and read the Luci_salotto_on.htm file using some of the IDE example code? The length of the file name might be an issue.

The first problem was that.
The name length was too long.

but after renaming the file names on L_on, L_off, C_on, C_off, B_on, B_off, S_on, S_off arduino gives the same problem.

 #include <SPI.h>
  #include <Ethernet.h>
  #include <SD.h>
   
  byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xAC, 0x68 };
  File htmlFile;
  //creao un oggetto server che rimane in ascolto sulla porta
  //specificata
  EthernetServer server(80);
  int ledPin6 = 6;
  int ledPin7 = 7;
  int ledPin8 = 8;
  int ledPin9 = 9; // LED pin  
  String readString;
   
  void setup()
  {
    pinMode(ledPin6, OUTPUT);
    pinMode(ledPin7, OUTPUT);
    pinMode(ledPin8, OUTPUT);
    pinMode(ledPin9, OUTPUT);
    Serial.begin(9600);
    if(!Ethernet.begin(mac)) Serial.println("dhcp failed");
      else {
         Serial.print("IP is ");
         Serial.println(Ethernet.localIP());
      }
    server.begin();
     if (!SD.begin(4)) {
          Serial.println("Errore nella inizializzazione della SD");
          return;    // init failed
      }
  }
   
  void loop()
  {
    readString = "";
    EthernetClient client = server.available();
    if (client) {
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();
          if (readString.length() < 100) 
          {
            readString += c; //CONCATENA I CARATTERI
          }
          if (c == '\n') {
            if(readString.indexOf("Luci") > 0){
              //variabile per pagina html
              String pagina = "";
              //INIZIO pagina HTML
              pagina = "<html><head><title> Luci Stanze </title></head><body>";
              if(readString.indexOf("Luci1=1") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("C_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin6, HIGH);
              }
              else if (readString.indexOf("Luci2=1") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("S_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin7, HIGH);
              }
              else if (readString.indexOf("Luci3=1") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("B_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin8, HIGH);
              }
              else if (readString.indexOf("Luci4=1") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("L_on.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin9, HIGH);
              }
              else if(readString.indexOf("Luci1=0") > 0){
                //PAGINA LUCI
                htmlFile = SD.open("C_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin6, LOW); 
              }
              else if (readString.indexOf("Luci2=0") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("S_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin7, LOW); 
              }
              else if (readString.indexOf("Luci3=0") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("B_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin8, LOW); 
              }
              else if (readString.indexOf("Luci4=0") > 0){ 
                //PAGINA LUCI
                htmlFile = SD.open("L_off.htm",FILE_READ);
                if (htmlFile) {
                  while (htmlFile.available()) {
                    char c = htmlFile.read();
                    pagina = pagina + c;
                    Serial.print(c);
                  }
                // close the file:
                htmlFile.close();
                }
                digitalWrite(ledPin9, LOW);   
              }
              //FINE pagina HTML
              pagina = pagina + "</body></html>";
              Serial.print(pagina);
              }
            else{
              //HOME PAGE
             
              htmlFile = SD.open("home.htm",FILE_READ);
              if (htmlFile) {
                while (htmlFile.available()) {
                  char c = htmlFile.read();
                  client.print(c);
                  Serial.print(c);
                }
              // close the file:
              htmlFile.close();
            }else Serial.print(" Errore nella lettura file "); 
            break;
          }
          }
        }else Serial.print(" Client non accessibile ");
      }
      delay(1);
      client.stop();
    }else Serial.print(" client ");
  }

is the underscore character a legal file name character in the 8.3 file system?

Yes, I tryed without it too. But it doesn't work.

Maybe you are running out of SRAM? This might cause it depending on the size of the file.

// delete this variable
        String pagina = "";

//and all of these
             pagina = pagina + c;

// and these
           pagina = pagina + "</body></html>";
              Serial.print(pagina);

Just a thought...

I've deleted them, but same problem. =(

Ok. Sometimes code doesn't work like you expect. You should place some Serial.println() calls in your code to determine where it is failing. Add these to start, then post the last few lines of your serial monitor when it fails.

Add these two calls in your loop function.

    EthernetClient client = server.available();
    if (client) {
      // add this when client connects
      Serial.println(F("Client connected"));
      while (client.connected()) {

// and later in the code    
      delay(1);
      // add this after disconnecting
      Serial.println(F("disconnected"));
      client.stop();