On Jan 17, 8:44 am, [EMAIL PROTECTED] (Zentara) wrote:
> On Wed, 16 Jan 2008 14:08:14 -0800 (PST), [EMAIL PROTECTED]
>
>
>
> (Turner) wrote:
> >On Jan 16, 11:50 am, [EMAIL PROTECTED] (Zentara) wrote:
> >> On Tue, 15 Jan 2008 19:18:02 -0800 (PST), [EMAIL PROTECTED]
>
> >I think you may have misunderstood me. I'm now using exclusively
> >IO::Select--I dumped threads entirely (it's not you, threads, it's
> >me). I don't use fork either, now--just IO::Select. I got the
> >impression that fork would be kind of redundant with IO::Select.
> >Here's a snippet of my IO::Select code:
>
> >Code:
> >=================================================================
>
> >sub start {
> >    use IO::Socket;
>
> >    my ($self) = @_;
>
> >    my $host = $self->{'host'};
> >    my $port = $self->{'port'};
>
> >    my $sock = new IO::Socket::INET(
> >                       LocalHost => $host,
> >                       LocalPort => $port,
> >                       Proto     => 'tcp',
> >                       Listen    => SOMAXCONN,
> >                       Reuse     => 1);
>
> >    use IO::Select;
>
> >    my $select_set = new IO::Select();
> >    $select_set->add($sock);     #Add listening socket to select
>
> >    while(1) {
>
> the while(1) looks like it may cause heavy load, usually
> it's something like
>
> while ( my @ready = $select->can_read() ) { .... }
>
> see below for a different sub
>
>
>
> >            my ($readables) = IO::Select->select($select_set, undef, undef, 
> > 0);
>
> >            foreach my $r (@$readables) {
> >                    if($r == $sock) {
> >                            my ($new_sock, $client_addr) = $r->accept();
> >                            $select_set->add($new_sock);
> >                            ...process new socket...
> >                    }
> >                    else {
> >                            if(defined(my $buf = <$r>)) {
> >                                    ..process $buf..
> >                            }
> >                            #Client closed connection
> >                            else {
> >                                    $select_set->remove($r);
> >                                    close($r);
> >                            }
> >                    }
> >            }
> >    }
> >}
>
> >===================================================================
>
> >I basically took that straight from the tutorial Robert Leibl linked
> >me to here. It handles mutiple (well, two) clients appropriately--
> >everything works as it should, just with a lot more CPU usage than it
> >should
>
> Try this:  (variables named slightly different)
>
> #!/usr/bin/perl
> use strict;
> use IO::Socket;
> use IO::Select;
>
> my $listen = IO::Socket::INET->new(
>                   Proto => 'tcp',
>                   LocalPort => 9192,
>                   Listen => 5,
>                   Reuse => 1) or die $!;
>
> my $select = IO::Select->new($listen);
>
> my @ready;
>
> while(@ready = $select->can_read) {
>    my $socket;
>    for $socket (@ready) {
>
>    if($socket == $listen) {
>        my $new = $listen->accept;
>        $select->add($new);
>        print $new->fileno . ": connected\n";
>    } else {
>        my $line="";
>        $socket->recv($line,80);
>        if($line eq "") {
>          print $socket->fileno . ": disconnected\n";
>          $select->remove($socket);
>          $socket->close;
>        };
>        print "$line\n";
>
>        my $socket;
>        for $socket ($select->handles) {
>        next if($socket==$listen);
>        $socket->send($line) or do {
>            print $socket->fileno . ": disconnected\n";
>            $select->remove($socket);
>            $socket->close;
>        };
>        }
>    }
>    }}
>
> __END__
>
> zentara
>
> --
> I'm not really a human, but I play one on earth.http://zentara.net/japh.html


Zentara, you are a lifesaver. That made it much better. I took the
while(1) idea from the linked tutorial, but it makes sense to loop on
a blocking read (the one time in this project that blocking is
useful). It's much better now, thank you so much.

Turner


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to