Arduino based Telnet Server disconnecting ?

Hi

I ve been using a Telnet Server on my Mega + Ethernet shield setup for some time now but I am experiencing erratic server disconnections just after the connection is established.

The following code is the telnet function which is part of a much bigger sketch doing other things as well.

I have also managed to replicate the issue with all other sketch functionality disabled.

The connection is lost at the line where the client.println statement "Just disconnected!" was inserted for debugging. In other words, the connection is established (prompt appears) and then disconnects with the last item shown on the client being ""Just disconnected!"

Please note that the above doesn't always happen. Repeated attempts to re-connect are eventually successful although sometimes a system reset is needed. Also this disconnection never happens during the first attempt following a system hard reset.

void telnet(); {

  if (telnetServer.available() && !connectFlag) {        
    connectFlag = 1;
    client = telnetServer.available();
    client.println(F("*** ACCESS RESTRICTED ***"));
    printPrompt();

  }

  if (connectFlag && client.available() == 0) {
    getReceivedText();
  }

  if (telnetClient.connected() == 0 && connectFlag) {
    client.println(F("Just disconnected!"));  // close connection if connection lost
    closeConnection();
  }
  if (connectFlag) {
    checkConnectionTimeout();
  }


}

you should not call telnetServer.available() in if. it expects to be assigned to EthernetClient

if (connectFlag && client.available() == 0) {
getReceivedText();
}

what do you get if available is 0?

I have

void telnetLoop() {

  static EthernetClient telnetClient = telnetServer.available();

  if (!telnetClient) {
    telnetClient = telnetServer.available();
  }
  if (telnetClient) {
    if (telnetClient.connected()) {
      while (telnetClient.available()) {
        int c = telnetClient.read();

if (connectFlag && client.available() == 0) {
getReceivedText();
}

what do you get if available is 0?

Sorry made a mistake in reproducing my code here.

The above was meant to be :

f (connectFlag && client.available() ) {
getReceivedText();
}

you should not call telnetServer.available() in if. it expects to be assigned to EthernetClient

Didn't understand what you meant here...

Watcher:
Didn't understand what you meant here...

if (telnetServer.available() && !connectFlag) {

telnet server available returns a reference to object. always. it is never zero. but it is possible it does the same as

EthernetClient telnetClient = telnetServer.available();
if (telnetClient && !connectFlag) {