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 | |
by haukex (Archbishop) on Oct 09, 2020 at 17:05 UTC | |
|
Re^2: How do I make a tcp socket heartbeat?
by pudda (Acolyte) on Oct 09, 2020 at 19:05 UTC | |
by haukex (Archbishop) on Oct 09, 2020 at 19:35 UTC |