in reply to How do I make a tcp socket heartbeat?

I see two issues with your code: First, you're not giving a timeout to the select (or rather, IO::Select) call, which is what you would need to do to implement the 10-second interval. Second, you've got the code for the heartbeat inside the loop over @$new_readable, however, if the select call times out, that variable will be undefined - please see those two documentation links for details. You will need to move the heartbeat code out of the loop, and detect timeout by setting and checking $! as described in the IO::Select docs.

Note that at the moment, your code does not cover a lot of the cases of select-based server code (e.g. errors or even the clients sending data). If you are still getting to that, great, I think that writing a complete server with select is a great learning experience for how things work on a relatively low level. OTOH, IMHO nowadays using an event loop is my personal favorite way to go, and at the moment, my favorite event loop is Mojo::IOLoop. I even wrote an example server and client and posted them here. A heartbeat can be implemented using Mojo::IOLoop->recurring.

Replies are listed 'Best First'.
Re^2: How do I make a tcp socket heartbeat?
by pudda (Acolyte) on Oct 09, 2020 at 16:54 UTC

    Thanks for the help!

    In fact, I'm pretty new to both perl and TCP connections. I used IO :: Socket because I found the most examples out there, but I found it very difficult to understand how everything works with it. Maybe in the future I will study more and try to do the script again, writing the complete server.

    I tested your example using Mojo and it worked very well, exactly as I wanted. Thanks very much!

      I used IO :: Socket because I found the most examples out there, but I found it very difficult to understand how everything works with it.

      Yes, the select(2) system call is (one of) the classic ways to do it. "Classic" both in the good and bad sense: good because understanding it will help you understand how lots of servers work and how things work on a lower level, not so good because nowadays I'd consider it fairly low level, so for just getting stuff done, as I said IMHO event loops are a good approach.

Re^2: How do I make a tcp socket heartbeat?
by pudda (Acolyte) on Oct 09, 2020 at 19:05 UTC

    Taking the opportunity, Could you tell me how I do if I wanted my client, when the server goes down, to try to reconnect?

    Like, in the $stream->on(close => ...) how do try to I connect again to the server?

      One way to do that is to place the Mojo::IOLoop->client call in a subroutine, as in sub reconnect { Mojo::IOLoop->client( ... ) }, make sure to call that sub once before starting the loop to set up the initial connection, and then, in the handler for the close event, do something like Mojo::IOLoop->timer( 2 => \&reconnect ); to call that subroutine to reestablish the connection. I very much suggest the timer call so as to not flood the server with connection attempts, and I would also recommend adding a counter variable so you can stop attempting to reconnect after a certain number of failed attempts.