8 print "$$ Server started\n";; # do a "top -p -H $$" to monitor server
10 our @clients : shared;
13 my $server = new IO::Socket::INET(
20 my $num_of_client = -1;
26 $client = $server->accept;
27 } until ( defined($client) );
29 my $peerhost = $client->peerhost();
30 print "accepted a client $client, $peerhost, id = ", ++$num_of_client, "\n";
31 my $fileno = fileno $client;
32 push (@clients, $fileno);
33 #spawn a thread here for each client
34 my $thr = threads->new( \&processit, $client, $fileno, $peerhost )->detach();
39 my ($lclient,$lfileno,$lpeer) = @_; #local client
41 if($lclient->connected){
42 # Here you can do your stuff
43 # I use have the server talk to the client
44 # via print $client and while(<$lclient>)
45 #print $lclient "$lpeer->Welcome to server\n";
48 # print $lclient "$lpeer->$_\n";
49 print "clients-> @clients\n";
51 foreach my $fn (@clients) {
52 open my $fh, ">&=$fn" or warn $! and die;
61 #close filehandle before detached thread dies out
63 #remove multi-echo-clients from echo list
64 @clients = grep {$_ !~ $lfileno} @clients;