That's exactly what I was trying to do: use a pool of pre-existing threads.
What I was hoping to achieve is to accept more than one connection at a time, as this script currently only handles one connection at a time.
I am planning on releasing this as part of a open source project, and as some people who might run this script (as part of the implementation) might have high traffic, this may not be very desirable, I think.

For example, simulating 100 connections from the bash shell locally on the server:
time for i in `seq 1 100`; do
perl -e 'printf "<policy-file-request/>%c",0' | nc myhost.com 9999
perl -e 'printf ">%c",0' | nc myhost.com 9999; done
takes 1.990 seconds

And I believe if I was to have say a pool of 3 threads (on a dual-processor server), I could easily handle 100 client connections per second

I extended the script quite a bit from a sample script offered by someone at Adobe, who also made another standalone script in python that supports more then one user connection (both scripts in the standalone folder of the sample scripts zip file at the head of the page):
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html

And I was hoping to extend it a bit more to thread just like the sample python script does, as to offer different variations (threaded or non-threaded perl scripts, python, java)

So, briefly, this is what I was trying to do (iirc):
$listeners = 3; my $Q = Thread::Queue->new; while ($running) { # Spawn listener threads for (1..$listeners) { threads->create(\&handleData)->detach; } # BEGIN LISTENING socket( LISTENSOCK, PF_INET, SOCK_STREAM, getprotobyname( 'tcp' ) +) or dienice("socket() error: $!"); setsockopt( LISTENSOCK, SOL_SOCKET, SO_REUSEADDR, pack( 'l', 1 ) ) + or dienice("setsockopt() error: $!"); bind( LISTENSOCK, sockaddr_in( $port, INADDR_ANY ) ) or dienice("b +ind() error: $!"); listen( LISTENSOCK, SOMAXCONN ) or dienice("listen() error: $!"); $log->warning("Listening on port $port"); while ( my $clientAddr = accept( CONNSOCK, LISTENSOCK ) ) { # put the connection on the queue $Q->enqueue(fileno(CONNSOCK), $clientAddr ); } } sub handleData { my $tid = threads->tid(); while (1) { my ($fno, $clientAddr) = $Q->dequeue(2); next if $fno eq 'STOP'; my ( $clientPort, $clientIp ) = sockaddr_in( $clientAddr ); my $ipStr = inet_ntoa( $clientIp ); open my $socket, '+<&=' . $fno; while (my $request = <$socket>) { chomp $request; if ($request eq '<policy-file-request/>') { $log->warning("($tid) : XML Policy file request from: +$ipStr"); print $socket $content.$NULLBYTE; close $socket; } else { $log->warning("($tid) : Connection from: $ipStr"); print $socket "<data><ip>$ipStr</ip></data>".$NULLBYTE +; close $socket; } }; #close $socket; } return 1; }


Then I run it, and simulate 2 connections (the handshake) from command-line shell like so:

perl -e 'printf "<policy-file-request/>%c",0' | nc myhost.com 9999 ; perl -e 'printf "%c",0' | nc myhost.com 9999

And it just hangs, instead of returning back to the shell. So I hit CTRL+C, then on the server log I see this show right after I CTRL+C'ed on the client connect simulation command:

August 27 10:33:44 xmlsockd[1722]: (1) : Connection from: x.x.x.x


And the server dies with no error/log messages :(

What am I doing wrong?

In reply to Re^2: Trying to thread a daemon with threads and Thread::Queue by jasmineaura
in thread Trying to thread a daemon with threads and Thread::Queue by jasmineaura

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.