in reply to Re^2: IO::Poll Examples?
in thread IO::Poll Examples?
Here's a very small IRC-like server example with no frills. Have several windows telnet into its port (or ports, it can handle several), and whatever line one telnet sends gets sent to all the other telnet clients.
#!/usr/bin/perl use IO::Socket; use IO::Poll qw( POLLIN POLLOUT ); use strict; $| = 1; my $poll = IO::Poll->new; $poll->mask($_ => POLLIN) for my @listens = map { IO::Socket::INET->new(LocalPort => $_, Listen => 9, Reuse => 1) or die "$@ opening socket on port $_" } my @ports = @ARGV ? @ARGV : (6666); my %listens = map { $_ => $_ } @listens; print "ready on @ports...\n"; while(1) { $poll->poll; for my $h ($poll->handles(POLLIN)) { if($listens{$h}) { my $new = $h->accept; $poll->mask($new => POLLIN); print "accepted from: ", $new->peerhost, ":", $new->peerport, "\ +n"; } elsif(sysread $h, my $in, 1024) { $listens{$_} or $h == $_ or print $_ $in for $poll->handles; } else { $poll->remove($h); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: IO::Poll Examples?
by Anonymous Monk on Jan 23, 2018 at 19:53 UTC |