My new web server example sketch has been updated to include a major bug fix. It had a problem with clients connecting and not sending any data, then not closing the connection. It would lock up a socket every time that happened, and after 4 times would use all the sockets and the server would no longer respond to requests.
Many thanks to CatweasleNZ for bringing this bug to my attention.
The new update will close any connected socket after 30 seconds. http://playground.arduino.cc/Code/WebServerST
The major change is the checkSockStatus() function at the end of the sketch.
The old server example at the bottom of that page does not have the new update.
If you use the new upgraded version, let me know how it does for you.
Peter_n:
My own webserver sketch has grown and I can't replace it with your code.
Is there a way to test my own sketch for that lock up ?
Sure. I used PuTTY to test my code. Connect to your Arduino IP/port (edit: in Raw mode), don't send anything, then disconnect your ethernet cable from either your computer or the ethernet shield before closing the PuTTY window. Leave it disconnected for about a minute. You will lose a socket. Do that 4 times and the server will no longer respond.
I use the ShowSockStatus() function in my example sketch to check the status of all sockets. As you perform that test with PuTTY, you will see the sockets become unavailable one at a time. The affected socket will show a status of 0x17 and will never release back to 0x0.
This is what I added to my example sketch.
#include <utility/w5100.h>
#include <utility/socket.h>
// add this to your global variables
unsigned long connectTime[MAX_SOCK_NUM];
// add this to your setup function
unsigned long thisTime = millis();
for(int i=0;i<MAX_SOCK_NUM;i++) {
connectTime[i] = thisTime;
}
// add this function and call it every iteration of your loop function
void checkSockStatus()
{
unsigned long thisTime = millis();
for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = W5100.readSnSR(i);
if(s == 0x17) {
if(socketStat[i] == 0x14) {
connectTime[i] = thisTime;
}
else if(socketStat[i] == 0x17) {
if(thisTime - connectTime[i] > 30000UL) {
Serial.print(F("\r\nSocket frozen: "));
Serial.println(i);
close(i);
}
}
}
else connectTime[i] = thisTime;
socketStat[i] = W5100.readSnSR(i);
}
}