Hello Perl gurus,

I'm currently in the process of writing a chat server in Perl.
Everything is all hunky-dory--it parses commands as it should, and is,
of course, quite satisfying. Except for one thing, and that is that it
cannot handle multiple clients at once, which, needless to say, is
kind of useful for a chat program, isn't it? So I've been following
the discussion online of Threads vs. forking vs. non-blocking IO, and
I've decided to try threads, which is neat because this is the first
thing I've ever done with threading. However, my excitement has been
somewhat dampened by the fact that it does not work. It can still
happily handle a single client--no complaints there. However, it can
still ONLY handle a single client. There's probably a hole in my
understanding of threads (e.g., I don't entirely understand what
join() and detach() DO...). Below is the relevant server code, and I
was hoping some kind soul could look at it, suppress his laughter at
my naive code and point me in the right direction.

Code:
============================================================
...
use threads;
...

sub start {
        use IO::Socket;

        my ($self, %args) = @_;

        my $host = $args{'host'} || $self->{'host'};
        my $port = $args{'port'} || $self->{'port'};

        my $sock = new IO::Socket::INET(
                           LocalHost => $host,
                           LocalPort => $port,
                           Proto     => 'tcp',
                           Listen    => SOMAXCONN,
                           Reuse     => 1);
        $sock or die "Unable to open a port on $host:$port: $!";
        print localtime() . ": Started server on $host:$port\n";
        $self->{'socket'} = $sock;
        $self->{'connected'} = 1;

        my ($new_sock, $client_addr);
        while(($new_sock, $client_addr) = $sock->accept()) {
                my $thread = threads->create(\&handleClient, $self, $new_sock,
$client_addr);
                $thread->join();
        }
}

========================================================

Thanks,
Turner


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


Reply via email to