I’m super new to the Particle platform but have experience in sockets programming. I am studying the TCPServer class. I am specifically studying the available() method. My testing seems to show that it doesn’t block but instead returns a TCPClient reference. If we have a new in-comming connection, then calling the connected() method on the TCPClient will return true.
A different world from the sockets API but different doesn’t mean wrong or bad. In sockets, I can define a backlog of connections that will be held by the TCP server when I call the sockets listen() API. When I then subsequently call the sockets accept(), then next available and waiting connection is returned to me.
I wanted to experiment and see what the story is with TCPServer. To that end I wrote the following app:
TCPServer server = TCPServer(9876);
TCPClient client;
uint8_t buffer[100];
void setup() {
Serial.begin(9600);
while(!Serial.available()) Particle.process();
Serial.printlnf("Listening on %s on port 9876", WiFi.localIP().toString().c_str());
server.begin();
}
void loop() {
if (client.connected()) {
if (client.available() > 0) {
int bytesRead = client.read(buffer, sizeof(buffer));
Serial.printlnf("Read %d bytes", bytesRead);
client.write(buffer, bytesRead);
}
} else {
client = server.available();
}
}
I then started creating connections to the Photon hosted server using multiple terminals on my Linux system and in each one ran the command:
nc 192.168.1.16 9876
I connected 1 client … all good. I connected 2 clients … all good. At 4 clients, still all good. Connecting the 5th client, something happened that I didn’t expect.
On connecting the 5th client, the 1st connected client was disconnected. On connecting the 6th client, the 2nd connected client was disconnected.
Before running this experiment, I was guessing at possible outcomes but this wasn’t one that I was at all expecting. My anticipation (based on sockets programming) would have been that the 5th client would have been rejected and the originally connected clients would continue. If one of them in the backlog was consumed then we would be able to backlog one more new connection.
Again, I’m not saying at all that anything is wrong here but it does call into question semantics of the APIs. Seeing that docs are open for editing, we could update the docs with more detailed semantics … but I feel that would be overly complex for the new readers.