[Pdx-pm] (no subject)

Fabio45K at netscape.net Fabio45K at netscape.net
Tue Mar 11 20:49:14 CST 2003

maybe you guys can help with this, here is some code i wrote for a server that i want to multithread, but i also want each client to be able to communicate. so i thought that sharing the $client variable would make it global to all threads and be able to have communication between threads, here is the code.


use strict;
use threads;        #using threads instead of fork!
use threads::shared;
use IO::Socket::INET;

$| ++;

#making the socket and server
my $listener = IO::Socket::INET->new
    LocalPort => 1337,
    Listen    => 5,
        Reuse     => 1
) || die "Cannot create socket\n";

warn "server up and ready for connections...... \n";
my $client : shared;
my $client_num : shared;
my $client_num = 0;
lock $client;

#create the threads
while (1) 
    my $client = $listener->accept;
    threads->create(\&start_thread, $client, ++ $client_num);

sub start_thread 
    my @grab;
    my ($client, $client_num) = @_;
    print "thread created for client $client_num\n";
    push @grab, $client;
    print @grab;
    print $client "Welcome to the Kamran's Test server!                                            \n";                                          


sub begin
    while(my $line = <$client>){
    print $line;
    print $client $line;

Try AOL and get 1045 hours FREE for 45 days!

Get AOL Instant Messenger 5.1 for FREE! Download Now!

More information about the Pdx-pm-list mailing list