use IO::Socket; use threads; use threads::shared; use Thread::Queue::Duplex; use strict; use warnings; # # create a TQD # create 2 threads # wait for them # my $tqd = Thread::Queue::Duplex->new(ListenerRequired => 1); my $thrdA = threads->new(\&threadA, $tqd); my $thrdB = threads->new(\&threadB, $tqd); $thrdA->join(); $thrdB->join(); print "Done.\n"; sub threadA { my $tqd = shift; $tqd->wait_for_listener(); # # open listen socket # pass fileno to other thread # my $listenfd = IO::Socket::INET->new( LocalPort => 9088, Proto => 'tcp', Listen => 10); die "Can't open listener: $!" unless $listenfd; # # now accept a connection # my $fd = $listenfd->accept(); # # !!!WRONG!!! # its enqueue_and_wait to block!!! # # my $resp = $tqd->enqueue('newfd', $fd->fileno()); my $resp = $tqd->enqueue_and_wait('newfd', $fd->fileno()); $listenfd->close(); return 1; } sub threadB { my $tqd = shift; $tqd->listen(); my $req = $tqd->dequeue(); my $id = shift @$req; my $fn = $req->[1]; print "fileno is $fn\n"; my $fd = IO::Socket::INET->new(); die "Can't acquire the socket: $!" unless $fd->fdopen($fn, '+>'); my $tpage = '

Got your click!!!

'; my $pglen = length($tpage); my $opage = "HTTP/1.0 200 OK Content-type: text/html Content-length: $pglen $tpage"; $fd->send($opage, 0); $fd->close(); $tqd->respond('newfd', 1); return 1; }