four-argument select()s

2001-03-27 Thread David Cantrell

Some of you will have seen me posting in #london.pm asking about the
four-arg form of select().  Some other people confessed ignorance too.
I eventually figgered it out by gratuitously copying and pasting from
POE::Kernel and then poking it to see how it broke :-)  For anyone who's
interested, here's a very simple bit of code which listens on three
sockets and as soon as you tickle one of them it dies telling you which
one it was.

#!/usr/bin/perl -w

use strict;
use IO::Socket;

my %ports=( # Associate some understandable names with socket numbers
test1 = 21000,
test2 = 21001,
test3 = 21002
);
my %sockets=(); # IO::Sockets will go here, with the same keys as above

foreach my $port (keys %ports) {  # create the IO::Sockets
$sockets{$port}=IO::Socket::INET-new(
Listen= 5,
LocalPort = $ports{$port},
Proto = 'tcp'
);
}

while(1) { waitForConnections(); }# wait for summat to happen

sub waitForConnections {
print "Waiting for connection ...\n";
my $rin=fhbits(values %sockets);  # get bit-mask
my $hits=select(
$rin,
undef, undef, # I'm only intersted in sockets becoming readable
10# Timeout
);
if($hits != -1) { # If select found something ...
# compare bitmask with file descriptors for each socket,
# and snarf any that have become readable
my @sockets=grep {
vec($rin, fileno($sockets{$_}),1)
} keys %sockets;
die(join("\n", @sockets));   # shout it to the world
}
}

sub fhbits { # calculate bitmask of all
my @fhlist=@_;   # IO::Handles passed as
my $bits=0;  # arguments
for (@fhlist) { vec($bits, fileno($_), 1)=1; }
$bits;
}

-- 
David Cantrell | [EMAIL PROTECTED] | http://www.cantrell.org.uk/david/

This is a signature.  There are many like it but this one is mine.

** I read encrypted mail first, so encrypt if your message is important **

 PGP signature


Re: four-argument select()s

2001-03-27 Thread Mark Rogaski

An entity claiming to be Robin Houston ([EMAIL PROTECTED]) wrote:
: 
: IO::Select will take the pain away.
: use IO::Select;
: 

However, when you start to throw signals, timers, and other nastiness into
the mix, I have found Event.pm to be terribly useful.

Mark

-- 
[]   | "Girls in occupied countries always
[] Mark Rogaski  | get into trouble with soldiers," she
[] [EMAIL PROTECTED] | said, when I asked her what the Virgin
[]   | birth was.  -- Florence King, CoaFSL

 PGP signature