TCPServer hangs after sometime

So I have photons running with latest 0.6.0 firmware (also tried with 0.5.3 with same behaviour) with TCPServer on the photon. The server accepts multiple clients at a time and stores it in an array of TCPClient. once the client has disconnected I replace the client in the array with NULL.

The problem that I am having is that the code works well for some time like 30mins or so (havent checked the repeatability of the time but happens sooner or later) and after that the TCPServer stops accepting any incoming clients. Feels like its hanging. Only after a reset when the server starts again does the communication resumes. During all this the cloud communication still works fine. This is causing delays in our deployment and I would appreciate any help.

You need to free the socket a disconnected client was attached to.
So before setting the client NULL, you may try calling client.stop()

This is also suggested here
https://docs.particle.io/reference/firmware/photon/#available--4

1 Like

Thanks for the quick response! I have updated the code and put it for testing. Hopefully this solves the problem. Fingers crossed.

1 Like

Worked! Thanks a lot ScruffR!

2 Likes

Hi All,

I am facing the same issue mentioned above. This happens in my case after few hours of inactivity. During that time there could be internet disconnects as well. I have to reset the photon and then the TCPServer starts to accepts connections from clients again. I do call client.stop() before setting the client to NULL. I even monitor the clients every 3 mins and if they are not connected then I stop them and set client to NULL.

My firmware version is 1.1.1 (latest) although I have been having the same issue since previous versions as well.

Meanwhile the cloud communication works perfect. Would appreciate pointing me in the right direction.

That’s some unorthodox behaviour.

First you post as the OP just to delete the post shortly after then and repost the same thing as a different user trying to tell us you are experiencing the same issue as you yourself confirmed solved two years ago.

I’m not sure what to make of that - but my gut feeling something smells fishy here

:slight_smile: nothing fishy. I did not realise before that I was posting from my personal account. So I deleted it and reposted from our developer account which I created today just so that communication stays sorted. Dint realise that could be an issue apart from the issue that we are really having :-):slight_smile:

Earlier had thought the issue was resolved but its cropped up from a few OS versions before.

Also when I searched the issue I did not realised I had only raised it many months back. My bad to have missed it.

If you are using a TCP or UDP listener, you need to monitor WiFi.ready(). When it becomes not ready and then ready again, you need to start the listener again by calling begin(port) again. You don’t need to stop it first.

Thanks @rickkas7. Have updated my code and uploaded on photon. Will post here after a few days of successful operation.

It been more than 72hrs and the problem looks to be resolved. But will observe for few more days and update here. Thanks @rickkas7!!

1 Like