When the server accept()'s a connection I add the connection id to a hash ...

What do you mean by "connection id"?

My assumption is that you mean an IO::Socket::INET handle. This is a blessed ref:

use IO::Socket::Inet;; $sock = IO::Socket::INET->new( PeerAddr => 'www.perl.org', PeerPort => 'http(80)', Proto => 't +cp' );; print $sock;; IO::Socket::INET=GLOB(0x1a05ec0)

which, for very good reason, you cannot put into a shared hash.

Basically, the methods that are attached to a blessed object like IO::Socket::INET=GLOB(0x1a05ec0) are tied to the thread in which the object handle (a GLOB in this case) is instantiated. So, if you share that object with another thread and that other thread attempts to call a method upon it, it will try to invoke code from the other thread. That cannot work, which is why Perl stops you from trying to do that.

There are two approached to solving this problem that I am aware of:

  • The first is "tried and tested", and is the method I described above. Each thread has a Thread::Queue associated with it and when it receives a message it posts a copy to each of the other threads and they send it to the client who's thread they are handling.

    This is easier to arrange by having a "controller" thread that also has a queue and each client forwards a copy of data it receives to the contoller via it's queue and it forwards it to each of the other clients.

  • The second is a little harder to explain, and whilst it has worked for me, is not (I believe) an officially sanctioned technique. That of re-blessing the handle into each thread that needs it.

    Again, this works best if a central controller thread is given access to handles from each of the client handles and it reblesses them. The clients would then forward their messages to the controller and it would forward them (directly) to the clients via the reblessed handles. This still leaves the problem of conveying those handles from the clients to the controller. Not insurmountable, but requires some jiggery pokery.

    All in all, the queues method is probably easier.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

    In reply to Re^5: share socket connections amongst multiple threads? by BrowserUk
    in thread 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.