ethernet shield+apache+php: client connect fails

Hello everybody. I have a very simple program that call a php file. the .ino is this:

"declaration...."

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(2000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /myFile.php HTTP/1.1");
    
    client.println("User-Agent: Arduino"); 
    client.println("Accept: text/html");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

the php code only write "php correctly called" once it's called. If I run the code many time resetting arduino this is what I obtain:

connecting... connected php correctly called disconnecting. connecting... connection failed

disconnecting. connecting... connected php correctly called disconnecting. connecting... connected php correctly called disconnecting.

So, sometimes but not always the "client.connect(server, 80)" return false... If i call many time the php script from a browser I have no problem so I suppose it is not a server problem because apache always answer correctly. Do you have any idea of why sometimes the client connect fails? Thanks in advance for your help.

If the server is a domain name, and not an ip, then you should send a "Host" in the header. I see you sent "Connection: close", so that normally infers HTTP/1.1. Note the change in the GET send.

    client.println("GET /myFile.php HTTP/1.1");
    client.println("Host: www.mydomain.com");    
    client.println("User-Agent: Arduino"); 
    client.println("Accept: text/html");
    client.println("Connection: close");
    client.println();

first of all thanks. I forgot to put HTTP/1.1 only in the code i put as example but the result is the same. No, is not a domain name but a server on my LAN, it is also strange that with the same code many time everything works and some time not. Can you explain me better when I need to use the "close" function? I have searched on the internet but not well understood when it is necessary to call it. Thanks

The code on this page will connect to the Google home page every 10 seconds. Don't do that long. Change the server to your server ip, and your network settings of course. http://arduino.cc/forum/index.php/topic,99629.msg754629.html#msg754629

The page is the second parameter in the getPage() call. Like getPage(server,"/myFile.php")

Does that do any better?

yes thanks. It works without problems. One question: why do you do the "client.stop();" every time and you do not leave the client for the next loop?

If it is a persistent connection, I don't close the connection. I do use those types of connections for things like transmitting control data from a joystick or the like. It is a bit harder to maintain that type connection.

Normal port 80 web traffic is a "connect-request-reply-disconnect" type communication. The server does not close the connection until it has sent the last packet, and the client code reads all the characters from the rx buffer. The client.stop on the client end tells the server you received everything and closes the connection.

thanks