Ethernet Shield Won't Close Connections

I have one controller which polls two irrigation controllers using http to obtain JSON (Java Script Object Notation) data which allows me to know which water valves are on. The program fine for 1-2 hours then finally the ethernet controller indicates that it cannot access the two irrigation controllers. After some diagnostic work, it seems the ethernet shield does not close connections properly and after a period of time it runs out of sockets to communicate with. The code attached the portion which handles the communication with the two irrigation controllers. Any assistance would be appreciated.

Here's a typical serial output at 4 minutes after startup:

Uptime: 4 Mins.

Connecting To House Controller.
Connection Status: 1

Socket#0:0x17 80 D:192.168.0.162(64260)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 1121 D:192.168.0.102(80)
Socket#3:0x0 0 D:0.0.0.0(0)

Flushing & Stopping House Controller.

Connecting To Coral Controller.
Connection Status: 1

Socket#0:0x17 80 D:192.168.0.162(64260)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 1122 D:192.168.0.104(80)
Socket#3:0x0 0 D:0.0.0.0(0)

Flushing & Stopping Coral Controller.

Here's some typical data once the failure has occurred:

Uptime: 73 Mins.

Connecting To House Controller.
Connection Status: 1

Socket#0:0x17 1532 D:192.168.0.104(80)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 2371 D:192.168.0.102(80)
Socket#3:0x17 2468 D:192.168.0.102(80)

Flushing & Stopping House Controller.

Connecting To Coral Controller.
Connection Status: 1

Socket#0:0x17 1532 D:192.168.0.104(80)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 2371 D:192.168.0.102(80)
Socket#3:0x17 2469 D:192.168.0.104(80)

Flushing & Stopping Coral Controller.

Uptime: 73 Mins.

Connecting To House Controller.
Connection Status: 0

Socket#0:0x17 1532 D:192.168.0.104(80)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 2371 D:192.168.0.102(80)
Socket#3:0x17 80 D:192.168.0.127(53599)

Initial Connection Request To Irrigation Controller Failed On House Controller.
Flushing & Stopping House Controller.

Connecting To Coral Controller.
Connection Status: 0

Socket#0:0x17 1532 D:192.168.0.104(80)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 2371 D:192.168.0.102(80)
Socket#3:0x17 80 D:192.168.0.127(53599)

Initial Connection Request To Irrigation Controller Failed On Coral Controller.
Flushing & Stopping Coral Controller.

code.ino (3.26 KB)

Additional information:

The .104 and .102 addresses are the irrigation controllers.
The .101 address is a time server.
The .162 address is a web browser polling the controller

Here's the code without having to download:

//Clear Polled, Irrigation & Zone Running Variables
polledZone = 0;
zoneRunning = 0;
controllerRunning = 0;

//Poll The Irrigation Controllers and find out whats running
for (controllerPointer=1; controllerPointer<3; controllerPointer++)
{

//Select IP address for the appropriate controller
if (controllerPointer == 1)
{
connectionStatus = client.connect(HouseController, 80);
Serial.println("Connecting To House Controller.");
}
else
{
connectionStatus = client.connect(CoralController, 80);
Serial.println("Connecting To Coral Controller.");
}

//Set Controller Status Flag
controllerStatus[controllerPointer] = connectionStatus;
Serial.print("Connection Status: ");
Serial.println(connectionStatus);

//Show Socket Status On Serial Monitor
ShowSockStatus();

//If connection successful to controller request JSON Date
if (connectionStatus == 1)
{
// Make a HTTP request:
client.println("GET /status.json HTTP/1.1");
client.println("Connection: close");
client.println();
}
else
{
// If you didn't get a connection to the server:
Serial.print("Initial Connection Request To Irrigation Controller Failed On ");
if( controllerPointer == 1)
{
Serial.println("House Controller.");
}
else
{
Serial.println("Coral Controller.");
}
}

// Read And Parse JSON Data
timeOut = millis(); //Set start time for break function
while (client.connected())
{
if(millis() - timeOut > 5000)
{
//Irrigation Controller Did Not Provide Data Within 5 seconds
Serial.print("JSON Time Out Fault On ");
if( controllerPointer == 1)
{
Serial.println("House Controller.");
}
else
{
Serial.println("Coral Controller.");
}
connectionStatus = 0; //Set flag for unsuccessful Connection
client.stop(); //Close Communication With Irrigation Controller
controllerStatus[controllerPointer] = -5; //Set Controller Status Flag
break;
}
while(client.available())
{

//Parse JSON Data To Retrieve Which Zone Is Running
if (client.available())
{
char c = client.read();
if(i<111)
{
json*=c;*

  • if(i == 109)*

  • {*

  • polledZone = (int)c - 48;*

  • }*

  • if (i ==110)*

  • {*

  • lastChar = (int)c; *

  • }*

  • i++;*

  • }*

  • }*

  • }*

  • }*

  • void ShowSockStatus()*
    {
    Serial.println();
    for (int i = 0; i < MAX_SOCK_NUM; i++) {

  • Serial.print(F("Socket#"));*

  • Serial.print(i);*

  • uint8_t s = W5100.readSnSR(i);*
    _ socketStat = s;_
    * Serial.print(F(":0x"));*
    * Serial.print(s,16);*
    * Serial.print(F(" "));*
    * Serial.print(W5100.readSnPORT(i));*
    * Serial.print(F(" D:"));*
    * uint8_t dip[4];
    _
    W5100.readSnDIPR(i, dip);_
    _
    for (int j=0; j<4; j++) {_
    _
    Serial.print(dip[j],10);_
    _
    if (j<3) Serial.print(".");_
    _
    }_
    _
    Serial.print(F("("));_
    _
    Serial.print(W5100.readSnDPORT(i));_
    _
    Serial.println(F(")"));_
    _
    }_
    _
    Serial.println();_
    _
    }f*_

You are not closing the connection from the client end after the server sends a close message.

while(client.connected()) {
  // do your download from the server here  
  while(client.available()) {
    char ch = client.read();
    Serial.write(ch);
  }
}

// then after the server closes its end, the client closes its end
client.stop();

I'm sorry. I did not copy all of the code. It does have the client.stop() as you will see below:

//Poll The Irrigation Controllers and find out whats running
for (controllerPointer=1; controllerPointer<3; controllerPointer++)
{

//Select IP address for the appropriate controller
if (controllerPointer == 1)
{
connectionStatus = client.connect(HouseController, 80);
Serial.println("Connecting To House Controller.");
}
else
{
connectionStatus = client.connect(CoralController, 80);
Serial.println("Connecting To Coral Controller.");
}

//Set Controller Status Flag
controllerStatus[controllerPointer] = connectionStatus;
Serial.print("Connection Status: ");
Serial.println(connectionStatus);

//Show Socket Status On Serial Monitor
ShowSockStatus();

//If connection successful to controller request JSON Date
if (connectionStatus == 1)
{
// Make a HTTP request:
client.println("GET /status.json HTTP/1.1");
client.println("Connection: close");
client.println();
}
else
{
// If you didn't get a connection to the server:
Serial.print("Initial Connection Request To Irrigation Controller Failed On ");
if( controllerPointer == 1)
{
Serial.println("House Controller.");
}
else
{
Serial.println("Coral Controller.");
}
}

// Read And Parse JSON Data
timeOut = millis(); //Set start time for break function
while (client.connected())
{
if(millis() - timeOut > 5000)
{
//Irrigation Controller Did Not Provide Data Within 5 seconds
Serial.print("JSON Time Out Fault On ");
if( controllerPointer == 1)
{
Serial.println("House Controller.");
}
else
{
Serial.println("Coral Controller.");
}
connectionStatus = 0; //Set flag for unsuccessful Connection
client.stop(); //Close Communication With Irrigation Controller
controllerStatus[controllerPointer] = -5; //Set Controller Status Flag
break;
}
while(client.available())
{

//Parse JSON Data To Retrieve Which Zone Is Running
if (client.available())
{
char c = client.read();
if(i<111)
{
json*=c;*

  • if(i == 109)*
  • {*
  • polledZone = (int)c - 48;*
  • }*
  • if (i ==110)*
  • {*
  • lastChar = (int)c; *
  • }*
  • i++;*
  • }*
  • }*
  • }*
  • }*
  • client.flush(); //Clear Data Buffer In Ethernet Controller*
  • client.stop(); //Close Communication With Irrigation Controller*
  • i=0; //Reset Parsing Pointer*

I don't really want to troubleshoot your code today. I don't have time. If this is from your client, then this shows you do not have any sockets remaining to attempt a connection to your servers. Any attempt at this point will fail.

Socket#0:0x17 1532 D:192.168.0.104(80)
Socket#1:0x22 8888 D:132.163.4.101(123)
Socket#2:0x17 2371 D:192.168.0.102(80)
Socket#3:0x17 80 D:192.168.0.127(53599)

Here is my client code on the playground. Maybe you should try it to see if that fails too.
http://playground.arduino.cc/Code/WebClient