Go Down

Topic: Handling Telnet Connect/Disconnect (Gracefully) (Read 1 time) previous topic - next topic

RayLivingston

Feb 06, 2016, 08:45 pm Last Edit: Feb 06, 2016, 11:08 pm by RayLivingston
I have an application that currently uses a Serial connection for its "console". I would like to replace this with a telnet connection instead.  I would like the client to be able to connect and disconnect at any time, without hanging the application.  However, I'm having trouble figuring out how to determine whether the client is actually connected or not.  It works fine for the first connection after a reset of the Arduino, but subsequent telnet sessions fail to connect.

I've had no luck trying to use the EthernetClient.connected() method, as it seems to always return false.  The only way I've been able to get anything to work is to use EthernetServer.available() to detect when a client first connects, then assume the session remains active forever after that.  That, obviously, is why I cannot disconnect and re-connect without resetting the Arduino.

So, what is the proper method to determine whether or not a client is connected in such a way that I can disconnect and reconnect at will, and still have a robust connection?

Regards,
Ray L.

RayLivingston

#1
Feb 07, 2016, 03:14 am Last Edit: Feb 07, 2016, 03:51 am by RayLivingston
The below logic seems to work as I want, but is it really the "right" way to do it?  It does handle connects and disconnects properly, but deleting and re-creating the server object seems awfully brute-force to me....

Code: [Select]

#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetServer.h>
#include <EthernetClient.h>

#define ETH_CS 10
#define SD_CS 4

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEB };
IPAddress ip(10, 1, 1, 23);

EthernetServer *server = NULL;
EthernetClient client;
boolean connected = false;

void setup()
{
// Disable SD card CS, so it doesn't bugger Ethernet
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
pinMode(ETH_CS, OUTPUT);
digitalWrite(ETH_CS, LOW);
pinMode(87, INPUT_PULLUP); // KLUDGE to make Ethernet work on Due!
pinMode(77, INPUT_PULLUP);

Ethernet.begin(mac, ip);
server = new EthernetServer(23);
server->begin();
// Allow time for Ethernet to initialize
delay(1000);
}


void loop()
{
if (!server)
{
server = new EthernetServer(23);
server->begin();
}
if (server->available())
{
if (!connected)
{
                       client = server.available();
       connected = true;
       while (client = server->available())
       {
        client.print((char)client.read());
       }
                }
}
else if (server)
{
delete server;
server = NULL;
        EthernetClient *c = new EthernetClient();
        client = *c;
        connected = false;
        }
}


Regards,
Ray L.

RayLivingston

Is there really nobody here who knows how to deal with this issue?  Does Arduino Ethernet operate on the assumption that the Ethernet connection will be there on startup, and never removed?  It seems to me there should be some very simple solution to this, but I just can't find it, and I don't know enough about Ethernet to know how to modify the EthernetServer or EthernetClient objects to correct this (obvious) deficiency....

Regards,
Ray L.

discom

Having the same kind of issues. Client Arduino connected to a server Arduino and when I pull the cable, client.connected() remains true even if the server Arduino is gone. :(

SurferTim

#4
Mar 21, 2017, 05:32 pm Last Edit: Mar 21, 2017, 08:45 pm by SurferTim
I have a simple example of a telnet/persistent server in the playground. I'm on my iPhone but later I'll post a link.

edit: Here is the link: http://playground.arduino.cc/Code/Telnet

Go Up