hi, all monks:
    I try to figure out a walk around for this problem, by
passing the socket descriptor to the worker threads through
unix domain, and here is my experimental scripts:  
the server side script:
#!/usr/bin/perl # server use strict; use warnings; use threads; use IO::Socket::UNIX; use IO::Socket::INET; use Socket::MsgHdr; $| = 1; threads->create( \&handleClient )->detach(); my $listenSocket = IO::Socket::INET->new( 'LocalPort' => '8888', 'Listen' => SOMAXCONN, 'Reuse' => 1, 'Proto' => 'tcp', ); while ( defined ( my $conSocket = $listenSocket->accept ) ) { my $un_ConSocket = IO::Socket::UNIX->new( 'Type' => SOCK_STREAM, 'Peer' => '/tmp/undomain', ); my $outMsgHdr = Socket::MsgHdr->new( 'buf' => 'some bytes' ); $outMsgHdr->cmsghdr( SOL_SOCKET, SCM_RIGHTS, pack ( "i", fileno ($conSocket) ) ); my $sentMsgLen; { no warnings qw( uninitialized ); $sentMsgLen = sendmsg( $un_ConSocket, $outMsgHdr ); } } ## end while ( defined ( my $conSocket... sub handleClient { unlink '/tmp/undomain'; my $listenSocket = IO::Socket::UNIX->new( 'Type' => SOCK_STREAM, 'Local' => '/tmp/undomain', 'Listen' => SOMAXCONN, ); my $unConSocket = $listenSocket->accept; my $inMsgHdr = Socket::MsgHdr->new( buflen => 128, controllen => +256 ); my $receivedMsgLen; { no warnings qw( uninitialized ); $receivedMsgLen = recvmsg( $unConSocket, $inMsgHdr); } print 'received msg:', $inMsgHdr->buf(), "\n"; my ( $level, $type, $data ) = $inMsgHdr->cmsghdr(); my $fileno = unpack ( 'i', $data ); open ( CLIENTSOCKET, '+<&=' , $fileno ); while ( my $line = <CLIENTSOCKET> ) { print CLIENTSOCKET $line or warn $!; print "thread ", threads->tid(), " received line: $line\n"; } } ## end sub handleClient
The client side script:
#!/usr/bin/perl # client use strict; use warnings; use IO::Socket; $| = 1; my $socket = IO::Socket::INET->new( 'Proto' => 'tcp', 'PeerAddr' => '127.0.0.1', 'PeerPort' => '8888', ) or die $@; my $msgOut = "hello, server\n"; while (1) { print $socket $msgOut; my $msgIn = <$socket>; print "client received echo:$msgIn\n"; sleep 1; } ## end while (1)
It didn't work well, the worker thread only can get the first message from the client, and then hold on
 while ( my $line = <CLIENTSOCKET> ) 
, and the client side also hold on
my $msgIn = <$socket>; 
I can't figure out why they both hold on readline operation.

thx 4 helpping ...

In reply to Re: How to pass client connections to the worker threads? by sunshine_august
in thread How to pass client connections to the worker threads? by sunshine_august

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.