Ethernet connect to DB works only the first time

I have an Uno R3 and the latest Ethernet shield. The program reads sensors and connects to a Mac Apache server and posts data to a mysql data base. This all worked when using a Wifly board. Now I need to make it work with Ethernet. On reset, it gets the DHCP address and posts the first sensor data successfully but not the following three. The database connection is lost and can't be reestablished after the first time. Client.connect(server,80) returns 1 the first time and 0's after that. I also tried connecting once and using client.connected() but that returns false after the first time.

//Send new row to Data Base
void sendToDB(Xducer sensor)
{
    DEBUG_PRINTLN("\nConnecting to DB...");
    DEBUG_PRINT ("DB connect return code: ");
    int8_t resp = client.connect(server,80);
    DEBUG_PRINTLN (resp);
    if (resp == 1 )  // connected successfully
    {
      updateDB(sensor);
      //This will hang if no response!
      while(!client.available());  //wait here for response
      while (client.available()) 
      {
        char c=client.read();
        DEBUG_PRINT(c);
        if(c == '\n') 
        {
          client.flush();
          break;
        }
      } //end while client available

    } // end if client connected  
    else //client not connected
    {
       DEBUG_PRINTLN("Client NOT connected");
    }           
} //end sendToDB

You should wait for the server to close the connection, then close from the client end.

This is the "Perfect World" (see note below) version. It has no timeout feature like the "Real World" version.

//Send new row to Data Base
void sendToDB(Xducer sensor)
{
    DEBUG_PRINTLN("\nConnecting to DB...");
    DEBUG_PRINT ("DB connect return code: ");
    int8_t resp = client.connect(server,80);
    DEBUG_PRINTLN (resp);
    if (resp == 1 )  // connected successfully
    {
      updateDB(sensor);

      while(client.conected())
      {
        while (client.available()) 
        {
          char c=client.read();
          DEBUG_PRINT(c);
        } //end while client available

      } // end while client connected  

      // close client end
      client.stop();
    }
    else //client connection failed
    {
       DEBUG_PRINTLN("Client NOT connected");
    }           
} //end sendToDB

You can find my client code here. It has a timeout feature that prevents the "while(client.connected())" loop from becoming an endless loop if the server stalls or the connection breaks.
http://playground.arduino.cc/Code/WebClient
Look for the connectLoop variable. It controls the timeout.

Note: "Perfect World" is the imaginary place where power doesn't brown out or fail, servers never stall, networks never get overloaded, ITs do not reset routers, backhoes never dig up ethernet cables, etc.

SurferTim,
Thanks your suggestion fixed the problem and I added the timeout. However, I am not seeing any OK message printed out from the server. Could be because I have a problem with program memory (I just ran out). Now I need to figure out what I can do without. Using a lot of libraries for Ethernet, 1 wire, dallas, DHT, lcd.... First time I have ever run out of program memory. I think I'll get back to the 'perfect world' now and go have a beer.

After you have that beer (I just had one), insure you are using the F() function in your code. Take a look at my code in the playground for examples. Like this:

Serial.println(F("disconnecting."));

The F() function will save you a bunch of SRAM.

edit: Use the F() function with all your static strings.

I Have a Mega 2560. I haven't come even close to using all my program memory.

SurferTim,
I got rid of DHCP and went with fixed ip. Dropped to 87% program space used. I have about 35% RAM left. When I turn off DEBUG flash used drops to 81% and RAM increases to about 58%.

I am using client.print( string ); to send data to the DB.
Should I be using sprint

sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);

as in your example instead?

That depends on what you are sending. sprintf works good for me because every call to client.print creates and sends a separate packet, so I want as few packets sent as practical. If you have a memory problem, you might want to stick with client.print.