Hello,
we have a TCPServer set up on Photon (2.0.0) and multiple clients (phones) connected to it over local wifi. We can keep only 5 connections opened (Photon’s limitation), so we are looking for a way how to detect and close the inactive connections in order to free slots for the other phones.
We tried two approaches:
- periodically check the TCP connection status via client.status() - in case it is 0, close the connection. This works when the connection is closed correctly by client, however doesnt work when the connection is closed incorrectly (lost wifi - phone cant ask Photon first to close the connection). The client.status() is 1 even when the phone is disconnected from the wifi.
Minimal example below - it writes client.status to Serial every second. So first, connect to it over telnet (you will see “1” printed over serial), then disconnect the device with open telnet from wifi and you will see “1” still coming over serial - Photon didn’t recognize the connection was closed.
#include "Particle.h"
// EXAMPLE USAGE
// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;
int last = 0;
void setup(){
// start listening for clients
server.begin();
// Make sure your Serial Terminal app is closed before powering your device
Serial.begin(9600);
// Wait for a USB serial connection for up to 30 seconds
waitFor(Serial.isConnected, 30000);
}
void loop(){
if (client.status()) {
//client connected, do nothing
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
if ((millis()-last) > 1000) { // every second print status info
last = millis();
Serial.println(String(client.status()));
}
}
- approach - we tried to implement kind of “keep alive” feature, sending data to the socket periodically and detect errors. This works, but it is very intensive on RAM as the data that were not sent are kept in buffers in RAM and each connection needs around 6kB of RAM. This RAM is unfortunately not freed after the connection is close in the code, so going this way means we run out of memory on Photon very quickly…