Receiving TCP packet freezes the application for a moment

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:

And this is my blueprint level:

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

Thank you!

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)…

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

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.

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…

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.

You can try several things

Thank you so much! that solved my problem.