No Socket available with WiFi Sheild R3

My Equipment:

Arduino Mega 2560 R3
Arduino WiFi Shield R3 with firmware upgrade. This has the H&D HDG204 chipset.
I’ve applied the “fix” to my WiFi library (WiFi shield client does not release socket on connect fail (with fix) · Issue #1720 · arduino/Arduino · GitHub)

Still can’t make more than 4 requests to server…

My Code:

#include <SPI.h>
#include <WiFi.h>

char ssid = “mySSID”; // your network SSID (name)
char pass = “myPASSWORD”; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(172,16,0,10);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue:
while(true);
}

/***

  • Initialize the Network
    */

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 5 seconds for connection:
delay(5000);

}

Serial.println(“Connected to wifi”);

/***

  • Display the Network Status
    */

printWifiStatus();

for(int i=0; i<10; i++)
{

Serial.print("\nStarting connection “);
Serial.print(i,DEC);
Serial.println(” to server…");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(“connected to server”);
// Make a HTTP request:
client.println(“GET / HTTP/1.1”);
client.println(“Host: 172.16.0.10”);
client.println(“Connection: close”);
client.println();
}

// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {

Serial.println();
Serial.println(“Client available…”);

char c = client.read();
Serial.write(c);
}

if (!client.connected())
{
Serial.println();
Serial.println(“Client not connected…”);
client.flush();
client.stop();
}

delay(500); // wait a while

} // end for

}

void loop()
{
// Do nothing forever, or until the power goes out…
}

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
}

It appears you may be leaving sockets open by not waiting to get the entire response from the server. Try something like this:

    Serial.print("\nStarting connection ");
    Serial.print(i,DEC);
    Serial.println(" to server...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET / HTTP/1.1");
      client.println("Host: 172.16.0.10");
      client.println("Connection: close");
      client.println();
   
      while(client.connected()) {
        while (client.available()) {
          char c = client.read();
          Serial.write(c);
        }
      }
      client.stop();
    }    
    delay(500);

Does this do any better?

Thanks, but I already have a "delay" in the code if you look after my connection.stop. I will try to increase the delay amount to see if that works.

If the wifi rx buffer is not empty, client.stop() may not close the connection, no matter how long that delay is. Did you see the change to the way the response is received? You must wait for the server to close the connection, then you close the client end, or use a timeout in the event the server stalls or the connection fails.

      while(client.connected()) {
        while (client.available()) {
          char c = client.read();
          Serial.write(c);
        }
      }
      client.stop();

That worked like a champ. Thank you so much!!!

I really need to slow down and look. Thanks for reminding me.

I was able to read 9 GET requests without issue after applying your modification.

Now on to POSTing... which is what I really need to be able to do. Keeping my fingers crossed.

Thanks again.

Here is my client code on the playground. There is both a GET and a POST example. They are for the ethernet shield, but once the wifi connection to the AP is established, the protocol is the same.
http://playground.arduino.cc/Code/WebClient

This worked for me too! Thanks very much!