V 0.3.1 - Core Stops responding to TCP/Telnet After disconnecting from Cloud

Hi,
I am facing this issue on V0.3.1 though the same code is working fine with Version 0.2.3. I am pasting a sample code:

TCPServer server = TCPServer(23);
TCPClient client;
int available;
unsigned long intTime;

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {

// start listening for clients
server.begin();

Serial.begin(9600);

Spark.connect();
delay(2000);

intTime = millis();

}

void loop() {

if (client.connected()) {
//Process incoming bytes first
available = client.available();
if (available > 0) {
server.write(client.read());
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}

if((millis() - intTime >=50000)){
RGB.control(true);
RGB.color(255, 0, 0);
Spark.disconnect();

}

}

Try connecting/disconnecting (via telnet) to core once it is disconnected from the cloud. It will connect for first couple of times and then will stop responding (No Route to Host…)

Formatted code from author… @amit_singh Please use the formatting options available when you create a topic. It improves your chances of someone helping you since they can understand your code better :smile:

TCPServer server = TCPServer(23);
TCPClient client;
int available;
unsigned long intTime;

SYSTEM_MODE(SEMI_AUTOMATIC);    

void setup() {    
    // start listening for clients
    server.begin();    
    Serial.begin(9600);    
    Spark.connect();
    delay(2000);    
    intTime = millis();    
}

void loop() {    
    if (client.connected()) {
        //Process incoming bytes first
        available = client.available();
        if (available > 0) {
            server.write(client.read());
        }
    }
    else { 
        // if no client is yet connected, check for a new connection
        client = server.available();
    }

    if((millis() - intTime >= 50000)) {
        RGB.control(true);
        RGB.color(255, 0, 0);
        Spark.disconnect();    
    }    
}

@amit_singh I’m not sure if this will solve the problem but it will definitely help. Close your previous tcpClient before you move onto the next tcpClient. Something tells me its not the disconnecting from the cloud that is causing the issues but you running out of sockets. So if you do a tcpClient.stop(), you will be able to reuse that socket.

Replace:

// if no client is yet connected, check for a new connection
client = server.available();

With:

// if no client is yet connected, check for a new connection
client.stop();
delay(20);
client = server.available();

Edit: Oops, I meant stop() and not close() :smiley: . Corrected the mistake. Thanks @amit_singh

Thanks @nitred. Will get the code formatted going forward. :smile:

I think you meant client.stop(); I have added that as well but behavior remains the same

Also, one observation, once disconnected from the cloud, core is getting removed from the router active list after few minutes which is not happening in case of version 0.2.3.

Try to do the connect and disconnect using telnet after core is not connected to the cloud and you may be able to reproduce this issue.