# Receiving TCP packet freezes the application for a moment

**URL:** <https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341>\
**Category:** C++\
**Tags:** unreal-engine, freezing, question, Blueprint, CPP\
**Created:** [February 24, 2021, 11:51am UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341 "2021-02-24T11:51:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![OXVII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OXVII](https://forums.unrealengine.com/u/OXVII)\
**Post date:** [February 24, 2021, 11:51am UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/1 "2021-02-24T11:51:04Z")

</div>

Hello everyone,

I made a C++ class for sending/receiving TCP packets and exposed it to blueprints via blueprint function library. The problem is that everytime when I call the blueprint to send/receive packets, the application freezes for a short time. I tried to make this class to estabilish a connection with the PLC via modbus TCP/IP, and I need to receive data continually, with a sampling period of about 0.2 sec, and this means that every 0.2 sec. application freezes.

I found that the function that receives the package is the one that freezes the program. It is the default function from WS2tcpip library:

 ![330710-code.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/7/a/87ab796157f4a090588c893799f6e33c91909f3b.png)

And this is my blueprint level:

 ![330709-bllevel.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/a/4/ca4a925720b873aba172521882210f3a110a6cc0.png)

Except this, everything works perfect.  
Can I do something to keep the application from freezing?

Thank you!

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 24, 2021, 3:39pm UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/2 "2021-02-24T15:39:50Z")

</div>

You can try to call ReadHoldingRegisters with Timer rather than with Tick. Another approach - to check out UE Tasks and Delegate cycling call template (delegate call func that call itself after while)…

---

<div class="post-metadata">

**Author:** ![OXVII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OXVII](https://forums.unrealengine.com/u/OXVII)\
**Post date:** [February 25, 2021, 8:20am UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/3 "2021-02-25T08:20:48Z")

</div>

That doesn’t solve the problem, it freezes the application even if I call ReadHoldingRegisters with a keyboard event, like this:

 ![330820-capture1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/9/0a9179add1ef68a1c9be304b3bc342c3d91de25a.png)

It behaves as if inside this recv() function exists a do-while loop, I had a similar problem on other application when I used inside my C++ code a do-while loop. But in this case my C++/socket knowledge is limited to understand what is going on inside this recv() function and at first sight it does not seem to exist a do-while loop. I saw that some succed to estabilish the TCP communication for this purpose, but I don’t know if someone used the same WS2tcpip.h file.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 25, 2021, 8:30am UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/4 "2021-02-25T08:30:04Z")

</div>

Ok, can you try to wrap this call into some non Game Thread with some Background Task? It seems that current implementation blocks one of main threads of UE…

---

<div class="post-metadata">

**Author:** ![OXVII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OXVII](https://forums.unrealengine.com/u/OXVII)\
**Post date:** [February 25, 2021, 8:46am UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/5 "2021-02-25T08:46:57Z")

</div>

Oh, I’m new to UE, can you please give me a clue where I can find out some information about how to do this? P.s I also think that this is the problem and thank you very much for trying to help me.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 25, 2021, 3:16pm UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/6 "2021-02-25T15:16:17Z")

</div>

You can try several things

- [Async | Unreal Engine Documentation](https://docs.unrealengine.com/en-US/API/Runtime/Core/Async/Async/index.html)
- [wiki.unrealengine.com](https://michaeljcole.github.io/wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4/)
- [https://www.orfeasel.com/implementing-multithreading-in-ue4/](https://www.orfeasel.com/implementing-multithreading-in-ue4/)

---

<div class="post-metadata">

**Author:** ![OXVII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OXVII](https://forums.unrealengine.com/u/OXVII)\
**Post date:** [February 26, 2021, 3:10pm UTC](https://forums.unrealengine.com/t/receiving-tcp-packet-freezes-the-application-for-a-moment/479341/7 "2021-02-26T15:10:11Z")

</div>

Thank you so much! that solved my problem.
