I attempted to extend the sample code I posted in Re: Transfer a hash using client and server in perl to make it do and handle concurrent clients. I tried this:

#! perl -slw use strict; use threads; use IO::Socket; use Storable qw[ freeze thaw ]; use Data::Dump qw[ pp ]; $|++; my %hash; @hash{ 'a'..'h' } = 1 .. 8; my $lsn = new IO::Socket::INET( Listen => 5, LocalPort => '12345' ) or die "Failed to open listening port: $!\n"; async{ print 'accept loop started'; while( my $c = $lsn->accept ) { print "Connect from $c"; binmode $c; async { while( my $cmd = <$c> ) { print "Got '$cmd'"; last if $cmd =~ m[^quit]; printf $c "%s", pack "N/a*", freeze \%hash; } print "Got quit from $c"; close $c; }->detach; } }->detach; sleep 2; for my $client ( 1 .. 2 ) { # async { print "Client $client started"; my $s = new IO::Socket::INET( 'localhost:12345' ) or die "Failed to connect to server: $!"; print "client $client connected"; binmode $s; for ( 1 .. 2 ) { print $s 'givemeit'; print 'Sent givemeit'; my $len; read( $s, $len, 4 ) or die "Read failed: $!"; $len = unpack 'N', $len; print "read length: $len"; my $hashStr; read( $s, $hashStr, $len ) or die "Read failed: $!"; print "Read hashstr length: ", length $hashStr; my %hash = %{ thaw $hashStr }; pp \%hash; } print $s 'quit'; close $s; print "client; $client disconnected"; # }->detach; } sleep 1 while 1;

Which works perfectly. It serially connects to the server (twice), transmits a couple of hashes and disconnects. The server then awaits further connection until ^C.

But if you uncomment the #async{ & }->detach; lines in order to allow the clients to run concurrently, nothing happens. The listener starts and awaits a connection. The clients loop is entered and the async() is dispatched, but the thread never starts and async doesn't return. No errors. Just nothing.

But the weird part is, if I connect to the listener from an external process (telnet), the client threads start, run concurrently and finish as designed.

So the question is, what is it about connecting from an external process that causes the in-process client threads to start?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

In reply to threads + sockets stalling question by BrowserUk

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.