I ve been working on a Telnet Server based on the code posted in the forum here.
It all works just fine, however if, for some reason (due to network issues or other), the connection to the client is lost, the program freezes and you are unable to reconnect unless a hard reset is performed.
I ve tried adding code that will disconnect the Server if connection is lost, eg:
if (!TelnetServer.available() && connectFlag) {closeConnection();}
Sounds like you need to do a bit of debugging. This function may help you troubleshoot the socket status. Call it at the points of your code that you have trouble with, like in loop.
#include <utility/w5100.h>
byte socketStat[MAX_SOCK_NUM];
void ShowSockStatus()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
Serial.print(F("Socket#"));
Serial.print(i);
uint8_t s = W5100.readSnSR(i);
socketStat[i] = 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(")"));
}
}
Status list:
0X0 = available.
0x14 = socket waiting for a connection
0x17 = socket connected to a client or server.
0x1C = open socket waiting for close
0x22 = UDP
You should have one socket with a status of 0x14 and at least one with a status of 0x0.
edit: Insure you start your serial I/O in setup also.
Serial.begin(9600);
Your begin call is not correct. You are missing a parameter.
// change this...
Ethernet.begin(mac, ip, gateway, subnet);
// to this
Ethernet.begin(mac, ip, gateway, gateway, subnet);
edit2: If you want to see a low level telnet server that can handle multiple clients simultaneously, take a look here. http://playground.arduino.cc/Code/Telnet
It doesn't have a timeout because I left that up to each user depending on the specific requirements for your application.