Arduino TELNET Server freezes when connection lost

Hi all,

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();}

but it didn't solve the problem.

Any help would be greatly appreciated.

Thanks

Any help would be greatly appreciated.

Sure.

anwersGeneratedHere();

How the hell are we supposed to know HOW you are closing the connection/resetting things?

I already said that the code was derived from an earlier post in the forum HERE. All the code and functions are there.

I already said that the code was derived from

"Was derived from" means "is not identical to" in my mind.

I ve tried adding code that will disconnect the Server if connection is lost, eg:

If you don't want to show your code, I'm fine with that. Good luck.

I was just trying to avoid reproducing code already on the forum. Here it is, attached due to size.

Using Arduino Mega + Ehternet shield.

telnet.ino (10.2 KB)

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.

Thanks for the help. I ll try it and let you know. Whats the second gateway?

The first "gateway" is a place holder for the dns server ip, but since server code rarely uses dns, you can use the gateway.

Ethernet.begin(mac, ip, dns, gateway, netmask);

So, i have tried out the above function for debugging and here are the results:

Before Telnet connection :

Socket#0:0x14 23 D:0.0.0.0(0)
Socket#1:0x22 8888 D:0.0.0.0(0)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)

During Telnet connection:

Socket#0:0x17 23 D:192.168.10.24(61189)
Socket#1:0x22 8888 D:0.0.0.0(0)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x14 23 D:0.0.0.0(0)

After simulated loss of connection and attempt of re-connection:

Socket#0:0x17 23 D:192.168.10.24(61189)
Socket#1:0x22 8888 D:0.0.0.0(0)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x17 23 D:192.168.10.24(61221)

Second attempt of re-connection:

Socket#0:0x17 23 D:192.168.10.24(61189)
Socket#1:0x22 8888 D:0.0.0.0(0)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x1C 23 D:192.168.10.24(61221)

My guess is that a socket remains open. Correct?

My guess is that a socket remains open. Correct?

Correct.

so, is it possible to manually close the socket after timeout?

Edit:

Got it!
inserted code line after timeout:
if(s == 0x1C) {
close(i);}
Now it all works fine.
Thanks very much for your help!