I have ran into an issue while creating a hash of socket id's and trying to share them across multiple threads. I get the error "Invalid value for shared scalar" due to the fact that the usage I have is trying to share an object reference (the socket id ref). This of course is not allowed but I can not think of an alternative to make this work.
#!/usr/bin/perl -w use strict; use threads; use threads::shared; use IO::Socket::INET; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '12345', Proto => 'tcp', Listen => 10, Blocking => 1, ); die "Could not create socket: $!\n" unless $sock; my($thread, $cnt); my %usersocks : shared; while (my $UserSock = $sock->accept()) { $cnt++; $thread = threads->create(\&connection, $UserSock, $cnt); } $_->join for threads->list; sub connection { my ($sockID, $cnt) = @_; my $sockData; lock(%usersocks); $usersocks{$cnt} = $sockID; while($sockID->recv($sockData, 1024)) { print $sockData; #debug code to see contents of hash while(my($key, $value) = each(%usersocks)) { print "$key => $value\n"; } $usersocks{$_}->send($sockData) for sort(keys %usersocks); } close($sockID); delete $usersocks{$sockID}; }

The line $usersocks{$cnt} = $sockID; is of course my issue because $sockID is the obj reference to the connection id. Is there a way of accomplishing this? I ultimately want all data sent to this server application to be sent out to all threaded clients.

In reply to share socket connections amongst multiple threads? by Elijah

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.