Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use IO::Select ; use IO::Socket; use constant BUFSIZE => 1024 ; sub logmsg { print "INFO: @_\n" } my $port = shift || 2345; my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => $port) ; my $sel = new IO::Select($lsn) ; my %inbuffer; my %ready ; my ($buf,$fh,$client,$new,$rVal) ; my (@items) ; logmsg "server started on port $port"; while (@items = $sel->can_read) { foreach $fh (@items) { # It's the Listen Socket if ($fh == $lsn) { $new = $lsn->accept ; $sel->add($new) ; # It's any Other Socket } else { $rVal = sysread($fh,$buf,BUFSIZE) ; unless (defined($rVal) && length $buf) { #Handle EOF print "EOF: $fh\n" ; delete $inbuffer{$fh}; $sel->remove($fh); close($fh) ; next ; } $inbuffer{$fh} .= $buf ; #Check for complete request if ($inbuffer{$fh} =~ /\n$/) { $inbuffer{$fh} =~ s/[\015\012]+$// ; push( @{$ready{$fh}}, $inbuffer{$fh} ); delete $inbuffer{$fh} ; } #Handle ready requests foreach $client (keys %ready) { handle($client); } delete $ready{$client}; } } } sub handle { # requests are in $ready{$client} my $client = shift; my $request; foreach $request (@{$ready{$client}}) { if ($request =~ /^quit$/) { print "Client: '$client' Requesting QUIT\n" ; $sel->remove($client); close($client) || die "Unable to Close '$client': $!\n" ; last; } print "Client: '$client' Req: '$request'\n" ; } delete $ready{$client}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cant find out how to get the real file handle name!
by japhy (Canon) on Apr 16, 2002 at 18:44 UTC | |
|
Re: Cant find out how to get the real file handle name!
by perlplexer (Hermit) on Apr 16, 2002 at 18:24 UTC | |
by Fletch (Bishop) on Apr 16, 2002 at 18:43 UTC | |
by mslattery (Initiate) on Apr 18, 2002 at 14:31 UTC | |
by mslattery (Initiate) on Apr 18, 2002 at 14:37 UTC | |
by mslattery (Initiate) on Apr 18, 2002 at 14:29 UTC |