Having sponged about the monastery looking for some trail-blazers re UDP Broadcasts. I cobbled together other people's code (can't remember which now.) to make a rough UDP broadcaster.

What is this supposed to do? I wanted to experiment with socket and IO::Socket to write a program that listens to a UDP port, and can send to the local broadcast address at the same port, all the while accepting abritary data from STDIN.

Pleased excuse the obscure variable and package names in places.
Package Trance has methods to listen to the broadcast address and to recv. It's input/output on the program side are a pair of thread queues,(never used threads in perl, and only once - deleteriously in python!!) the idea of thread queues I really liked!!

package noid is just a here to test the serialization of a class (forgive if these terms are wrong) , but from what I gather - the data structure of the noid object as it is recieved/thawed , is same as when it is created.

So now I sally forth to actually find out something about network/socket programming beyond the bear::necessities.

Now that my nodes can chatter on the network, It's time to write a proper interface.
submersible_toaster
-- bashing buttons.

#!/usr/dev/perl/bin/perl -w require 5.8; package Trance; use strict; use IO::Socket; #use Storable qw/freeze thaw/; $|=1; my $bcast='10.42.143.255'; my $port=4478; sub new { my $self = shift; return $self; } sub listen { my $self = shift; my $queue = shift; my $sock = new IO::Socket::INET ( LocalPort => $port, Proto =>'udp' ) || die "pulsar §$!"; my $flags; my $data; do { $sock->recv($data , 65536 , $flags); print "Recieved!"; $queue->enqueue( $data ); print " and Queued.\n"; $flags=undef; } until ($SIG{INT} ); } sub send { my $self = shift; my $queue = shift; do { my $data = $queue->dequeue; socket( CON , PF_INET , SOCK_DGRAM , getprotobyname('udp')) || die "s +ocket: $!$@"; setsockopt( CON , SOL_SOCKET , SO_BROADCAST , 1 ) || die "setsockopt: + $!$@"; my $dest = sockaddr_in( $port , inet_aton($bcast) ) ; send ( CON , $data , 0 , $dest ) || die "send $!"; close CON; } until ( $SIG{INT} ); } package noid; sub new { my $self= shift; my %hash = @_; return bless \%hash, $self; } package main; use Data::Dumper; use Storable qw/freeze thaw/; use threads; use threads::shared; use threads::shared::queue; my $nodeID = `hostname`; chomp($nodeID); my $inQueue = new threads::shared::queue ; my $outQueue = new threads::shared::queue ; my $trance; $trance = Trance->new; my $hear = threads->create ( 'pickup' ); my $tell = threads->create ( 'send' ); my $face = threads->create ( 'input' ); do { my $packet = ( $inQueue->dequeue ); print Dumper thaw $packet if $packet; } until ($SIG{INT}) ; sub pickup { $trance->listen( $inQueue ) }; sub send { $trance->send( $outQueue ) }; sub input { do { $_ = <STDIN>; chomp; my $time = time; my $ref = noid->new( pid => $$, time => $time, node => $nodeID, data => $_ ); $outQueue->enqueue(freeze $ref); } until ( $_ eq ''); }

In reply to UDP Broadcaster by submersible_toaster

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.