I wish there was less dissing each other and more coding in this thread, but that seems to be how it is when it comes to threads here ...

As usual I think threading isn't the best approach here as it is prone to subtle errors. That's not to say the following doesn't contain any subtle errors either but that's more likely because I didn't fully understand the specification. Other than that it seems to work fine. If you're unfamiliar with POE: it's an event based framework; each of the hash keys in the inline_states parameter to POE::Session->create is a named event handler. _start gets called first and opens four sockets and at the same time fires off a heartbeat event. The heartbeat handler first starts a new timer to be called again after 10 seconds with the same server port as an argument, then it sends the "\x01" message. Whenever a socket receives something, input is called and receives an array of inputs (usually only one) in $_ARG0; you'd have to flesh this one out of course. Note that messages currently have a maximum length of 1500 bytes, if that's too little you have to fix the corresponding literal in POE::Wheel::UDP. I don't know about your server but as problems with fragmented UDP packets seem to be rather common, datagrams are rarely made any bigger than that though---which was probably the author's idea for fixing the maximum at this value too.

use strict; use warnings; use POE; use POE::Wheel::UDP; use POE::Filter::Stream; my $SERVER_ADDR = '127.0.0.1'; my $LOCAL_ADDR = '127.0.0.1'; my %PORTS = ( 8020 => 53036, 8019 => 53037, 8008 => 53038, 8003 => 530 +39); POE::Session->create( inline_states => { _start => sub { my ($kernel, $heap) = @_[KERNEL, HEAP]; while(my ($sport, $lport) = each %PORTS) { $heap->{"port$sport"} = POE::Wheel::UDP->new( LocalAddr => $LOCAL_ADDR, LocalPort => $lport, PeerAddr => $SERVER_ADDR, PeerPort => $sport, InputEvent => 'input', Filter => POE::Filter::Stream->new, ); $kernel->yield(heartbeat => $sport); } }, input => sub { my ($input) = $_[ARG0]; foreach my $msg (@{$input->{payload}}) { print "Message from $input->{addr}:$input->{port}: '$m +sg'\n"; } }, heartbeat => sub { my ($kernel, $heap, $sport) = @_[KERNEL, HEAP, ARG0]; $kernel->delay_add(heartbeat => 10, $sport); $heap->{"port$sport"}->put( { payload => [ "\x01" ] }); print "sent HB to $sport\n"; }, } ); POE::Kernel->run;

In reply to Re: UDP connection by mbethke
in thread UDP connection by falstad

Title:
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, details, 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, summary, 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.