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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |