IO::Socket::INET and IO::Select are pretty easy to use and don't require a whole lot of code to get a lot done, I would suggest stick with them (though there may be a module out there that makes this even simpler.) You just need something like:

Observer

use strict; use IO::Socket::INET; use IO::Select; my $sock = IO::Socket::INET->new(LocalPort => 4455, Listen => 5); my $select = IO::Select->new($sock); my %gremlins; # list of gremlins! for(;;) { #mainloop if(my @socks = $select->can_read(0)) { #non-blocking check for dat +a for my $gremlin (@socks) { if($gremlin eq $sock) { # connection my $c = $sock->accept(); next unless $c; my $init = <$c>; chomp $init; $gremlins{$c} = [$c,$init]; # can't store actual ref a +s key print "Added gremlin $init\n"; $select->add($c); # add to list of sockets } else { if($gremlin->eof()) { #child died! print "Gremlin $gremlins{$gremlin}[1] died\n"; $select->remove($gremlin); delete $gremlins{$gremlin}; next; } #message for you, sire! my $msg = <$gremlin>; chomp $msg; print "Got message '$msg', sending to gremlins\n"; for(keys %gremlins) { next if $_ eq $gremlin; #dont send the message to +sender my $c = $gremlins{$_}[0]; #sock of peer print $c "$gremlins{$_}[1]: $msg\n"; #send to othe +r gremlins everything we get, as an exercise } } } } sleep 1; }
and Gremlin
use strict; use IO::Socket::INET; use IO::Select; my $sock = IO::Socket::INET->new(PeerAddr => "localhost:4455"); print $sock "Pid $$\n"; # the init string, used as a 'name' for this c +lient, you may want something more descriptive/informative etc. my $select = IO::Select->new($sock); my $i = 0; for(;;) { #main loop unless(int(rand(5))) { my $msg = "It has been $i iterations since I last sent somethi +ng\n"; print "Sending message: $msg"; print $sock $msg; $i=0; } if($select->can_read(0)) { #could capture sockets, but there is on +ly one die "observer gone\n" if $sock->eof(); print "Got a message:\n ".<$sock>; } $i++; sleep 1; }

Now, I just threw these together, but they do work. You would obvoiusly have to work your own code in here somehow rather than my sleep 1;

Also, if you look closely, you'll see where I didn't put any real error checking :) I leave that to you ;)

this should give you a start. You could have your gremlins connect to each other if they need to chat with each other, the server model is probably simpler, though.

make sense?

                - Ant
                - Some of my best work - (1 2 3)


In reply to Re: Recommend quick & dirty IP communications module? by suaveant
in thread Recommend quick & dirty IP communications module? by pileofrogs

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.