I liked this question, because it seems like it is a useful feature to have....broadcasting to all threads. I was hoping one of the experts would have handed us an answer, but not yet. :-) So I hacked on it for awhile, and narrowed it down to a problem where a global array is really not global. So the code below is where I left it, I hit a road block. Basically I made a "msg" sub, that when any thread typed "msg somestring" it should be broadcast to the other threads.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.