Well I did get some limited success, where if the message was sent from the last thread created, it would send it to all threads, because the copy of @clients it gets is the only one which contain all the previous clients. I tried it a second way, getting all threads with threads->list, but was getting an error when I tried to print to them, "Not a glob reference.....". :-(
So what I was doing to test my code was start the main server, then start 3 xterms and telneting in from each of them with "telnet 127.0.01 1337". And if I typed "msg 333333333" from the last created xterm, it would broadcast. But xterms of an earlier creation would not write to xterms of later creation. I tried refering to @clients as "main::@clients" but no-good.
#!/usr/bin/perl use strict; use threads; use IO::Socket::INET; use Data::Dumper; $| ++; 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"; our $hold; our $client; our $m = 2; my $client_num = 0; our @clients; #seems global, but isn't my $broadcastmessage; while (1){ our $client = $listener->accept; threads->create(\&start_thread, $client, ++$client_num); push(@clients,$client); print Dumper([@clients]),"\n"; } sub start_thread{ our $st = 100; our $hp = 100; my ($client, $client_num) = @_; push(@clients,$client); print "thread created for client $client_num\n"; print $client "\t\t\t****** ****** ********\n\r"; print $client "\t\t\t* * * * *\n\r"; print $client "\t\t\t* * * * *\n\r"; print $client "\t\t\t****** * * * *****\n\r"; print $client "\t\t\t* * * * *\n\r"; print $client "\t\t\t* # ****** # ******** #\n\r"; print $client "Welcome to the POG Test server!\n\r"; print $client "you will be know as player $client_num\n\r"; print $client "type : HELP for list of commands\n\r"; &begin; return; } sub begin{ while(our $line = <$client>){ print $line; if($line =~ /slp/i){&slp;} if($line =~ /med/i){&med;} if($line =~ /kik/i){&kik;} if($line =~ /help/i){&help;} if($line =~ /wke/i){&wke;} if($line =~ /hp/i){&hp;} if($line =~ /pun/i){&pun;} if($line =~ /st/i){&st;} if($line =~ /msg/i){&msg($line);} } } sub hp{ if (our $m eq 1){ print $client "you are sleeping/meditating.\n\r"; &begin; } print $client "your health is: "; print $client our $hp; print $client "\n\r"; } sub st{ if (our $m eq 1){ print $client "you are sleeping/meditating.\n\r"; &begin; } print $client "you stamina is: "; print $client our $st; print $client "\n\r"; } sub slp{ if (our $m eq 1){ print $client "you are already sleeping/meditating.\n\r"; &begin; } our $time = time; our $m = 1; print $client "you fall into a deep slumber.....\n\r"; &begin; } sub med{ if (our $m eq 1){ print $client "you are already sleeping/meditating.\n\r"; &begin; } our $time = time; our $m = 1; print $client "you sit down cross-legged on the floor and press your p +alms together, you s &begin; } sub wke{ if (our $m eq 2){ print $client "you are already awake\n\r"; &begin; } our $time2 = time; our $check = our $time2 - our $time; our $hp = $hp + $check; our $m = 2; print $client "you wake up from your deep sleep, feeling very refreshe +d!\n\r"; &begin; } sub kik{ if (our $m eq 1){ print $client "you are sleeping/meditating.\n\r"; &begin; } print $client "You kick yourself in the shin, and loose 10 HP\n\r"; our $hp = $hp - 10; if(our $hp < 1) { print "$client_num has died......\n\r"; print $client "$client_num has died......\n\r"; &start_thread; } &begin; } sub pun{ if (our $m eq 1){ print $client "you are sleeping/meditating.\n\r"; &begin; } print $client "You punch yourself in the face, and loose 10 HP +\n\r"; our $hp = $hp - 10; if(our $hp < 1){ print "$client_num has died......\n\r"; print $client "$client_num has died......\n\r"; &start_thread; } &begin; } sub help{ print $client "*********************************************** +******************** print $client "MED = Allows you to begin meditating, which regains st +amina print $client "SLP = Allows you to sleep, which regains health print $client "WKE = Wakes you up from sleeping or meditation print $client "KIK = Preforms a kick at the first living thin +g you see print $client "HELP = brings you back to this menu print $client "HP = prints your Health print $client "PUN = preforms a punch at the first living thing you s +ee print $client "ST = Prints your stamina print $client "******************************************************* +******************** &begin; } sub msg{ $broadcastmessage = shift; my $tid = threads->tid; print "caller-> $tid\n"; #works but only from last thread created #can't figure out how to make @clients truly global print "@clients\n"; foreach my $client (@clients){ print $client "$broadcastmessage\n"; } #gives all the threads, but gives error # "Not a glob reference when tryint ot print to it #my @list = threads->list; # print "list-> @list\n"; # foreach my $clientz (@list){ # my $tid = $clientz->tid,"\n"; # my $client = threads->object($tid); # print $client "$broadcastmessage\n"; # } return; }
In reply to Re: writing to all threads
by zentara
in thread writing to all threads
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |