in reply to How to pass client connections to the worker threads?
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:
The client 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
It didn't work well, the worker thread only can get the first message from the client, and then hold on#!/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)
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 ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to pass client connections to the worker threads?
by betterworld (Curate) on Sep 10, 2008 at 12:30 UTC | |
by sunshine_august (Scribe) on Sep 11, 2008 at 02:19 UTC |