I have a task where I'm waiting for connections in one thread and forward them to another thread. Maybe I something misunderstood but why I cannot reopen file descriptor in main thread?
Very simplified code:
sub inthread {
$socket = new IO::Socket::INET ( ... );
# ...
while ( my $client = $socket->accept() ) {
#if i print STDOUT, (fileno $client); # there i'll get 4
push ( @shared, fileno $client);
}
}
sub mainthread {
if ( scalar @shared ) {
$fd = shift @shared;
# if i print (fileno $fd); # there i'll get 4
open my $f, "<&=$fd" or die " error $!"; ### error here: Can't open 4: Bad file descriptor
while ( <$f> ) { ... }
}
}
threads->create(\&inthread);
while(1) { mainthread; }
whole code is there http://dpaste.com/3381RKV
test:
perl -w ./testcode.pl --port=10102 & wget -O - http://127.0.0.1:10102/