Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm glad you have a working solution using one of the async libs, because that's the way it should be done. However, here's a semi-tested version using just IO::Socket and IO::Select as an example of one way it could have been done.

#!/usr/bin/perl use strict; # use warnings; use IO::Socket; use IO::Select; my $port = 5000; my $hello = "Hello <VERSION>\n"; my $interval = 10; my $heartbeat = "HeartBeat\n"; my $listen = IO::Socket::INET->new( LocalPort => $port, Listen => 10, Reuse => 1, ) or die $@; my $sel = IO::Select->new($listen); my %clients; while( 1 ) { for my $fh ( $sel->can_read(1) ) { if( $fh == $listen ) { my $client = $listen->accept; $clients{$client} = { client => $client, heartbeat => time + $in +terval }; $sel->add( $client ); print $client $hello; } elsif( sysread $fh, my $buf, 4096 ) { print "got: $buf"; } else { $sel->remove( $fh ); delete $clients{ $fh }; } } for my $ref ( values %clients ) { if( time >= $ref->{heartbeat} ) { print { $ref->{client} } $heartbeat; $ref->{heartbeat} = time + $interval; } } }

I tested it with multiple telnets as clients and it happily heartbeats away, even with clients coming and going...

In reply to Re: How do I make a tcp socket heartbeat? by tybalt89
in thread How do I make a tcp socket heartbeat? by pudda

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2021-04-22 03:49 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found