Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-23 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found