# TCPServer hangs after sometime

**URL:** <https://community.particle.io/t/tcpserver-hangs-after-sometime/29421>\
**Category:** Troubleshooting\
**Created:** [February 20, 2017, 3:09pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421 "2017-02-20T15:09:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![neil.savant](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@neil.savant](https://community.particle.io/u/neil.savant)\
**Post date:** [February 20, 2017, 3:09pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/1 "2017-02-20T15:09:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 20, 2017, 3:19pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/2 "2017-02-20T15:19:50Z")

</div>

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](https://docs.particle.io/reference/firmware/photon/#available--4)

> [@](#):
>
> The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().

---

<div class="post-metadata">

**Author:** ![neil.savant](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@neil.savant](https://community.particle.io/u/neil.savant)\
**Post date:** [February 20, 2017, 3:28pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/3 "2017-02-20T15:28:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![neil.savant](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@neil.savant](https://community.particle.io/u/neil.savant)\
**Post date:** [February 21, 2017, 1:17pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/4 "2017-02-21T13:17:47Z")

</div>

Worked! Thanks a lot ScruffR!

---

<div class="post-metadata">

**Author:** ![Bluefalcon](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bluefalcon/32/22256_2.png) [@Bluefalcon](https://community.particle.io/u/Bluefalcon)\
**Post date:** [July 13, 2019, 3:02pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/6 "2019-07-13T15:02:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 13, 2019, 3:08pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/7 "2019-07-13T15:08:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bluefalcon](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bluefalcon/32/22256_2.png) [@Bluefalcon](https://community.particle.io/u/Bluefalcon)\
**Post date:** [July 13, 2019, 3:45pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/8 "2019-07-13T15:45:05Z")

</div>

🙂 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 :-)🙂

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

---

<div class="post-metadata">

**Author:** ![Bluefalcon](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bluefalcon/32/22256_2.png) [@Bluefalcon](https://community.particle.io/u/Bluefalcon)\
**Post date:** [July 13, 2019, 3:52pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/9 "2019-07-13T15:52:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [July 13, 2019, 7:22pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/10 "2019-07-13T19:22:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bluefalcon](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bluefalcon/32/22256_2.png) [@Bluefalcon](https://community.particle.io/u/Bluefalcon)\
**Post date:** [July 14, 2019, 9:03am UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/11 "2019-07-14T09:03:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bluefalcon](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bluefalcon/32/22256_2.png) [@Bluefalcon](https://community.particle.io/u/Bluefalcon)\
**Post date:** [July 23, 2019, 6:07pm UTC](https://community.particle.io/t/tcpserver-hangs-after-sometime/29421/12 "2019-07-23T18:07:44Z")

</div>

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