TCP Server and threading/callback/event handling

Hi,

I’m pretty new to open frameworks. I’m trying to create a TCP server but I’m listening for new messages in the wrong place. I want to basically:

Via setup make a callback or event handler to wait for incoming tcp messages and handle connect/disconnect

This callback or event handler will wait for new messages, when available copy them to a vector accessible in anywhere in the app.

In Update read the last 512 numbers from the stream which will be stored in a vector. Then I can use this to do calculations or use it in Draw.

Right now I’m checking for a new message in Update which is problematic because it’s only running every 1/30th or 1/60th of a second then acknowledging which is hanging my program sending the tcp stream.

Using ofThread and putting my TCP routines inside it worked. Then calling the threaded class from the main setup works well.

Hey! Having very similar problem and asked same question couple days ago.
Glad that it worked for you, will try to move TCP handling in a separate thread…

Beside managing TCP in threads, another thing that works so good for me is: don’t use standard openFrameworks ofx addons for networking. Use instead Sockets C/C++ standard library. You may put sendto(…) or recvfrom(…) in a while loop of a thread and set a callback or a signal to manage your subsequential actions (read the buffer / whatever) after data is succesfully sent or received. That’s a “million” times faster and, if you write it good, you’re guaranteed you don’t lose data, like when you’reading too slowly from update() and most of times there are silent buffer overflows or overwrites. AFAIK it works straightforward in macOs and Linux, in Windows there’s some quirky stuff to implement but it works there too.