Hi All,

Is there a way to pass a "socket descriptor" to another thread?  

Here is an example of what is being attempted here:

#!/usr/bin/perl

use strict;

use threads;
use Socket;

my $listener_thread = threads->new(\&listener);
$listener_thread->join;

sub listener {
       my $prot = getprotobyname("TCP");
       my $port = 11444;
       my $socket;
       socket $socket, PF_INET, SOCK_STREAM, $prot;
       setsockopt $socket, SOL_SOCKET, SO_REUSEADDR, pack("l", 1);
       bind $socket, sockaddr_in($port, INADDR_ANY) ;
       listen $socket, SOMAXCONN;
       my $stream;
       print "listener: Waiting for connections on port $port.\n";
       while ( accept $stream, $socket ) {
               print "listener: New incoming connection.\n";
               print $stream "listener: Connected!\n";
               my $ph = threads->new(\&protocol_handler, $stream);
               $ph->detach;
       }
}

sub protocol_handler {
       my $stream = shift;
       print $stream "handler: Waiting for input.\n";
       while ( my $message = <$stream> ) {
               print "handler: $message";
               print "handler: Sending 'Okay now'\n";
               print $stream "Okay.\n";
       }
}

Here is what is happening on the server side:
% ./listener_test
listener: Waiting for connections on port 11444.
listener: New incoming connection.
handler: hello
handler: Sending 'Okay now'

And here is what is happening at the client side using telnet:
% telnet 0 11444
Trying 0.0.0.0...
Connected to 0.
Escape character is '^]'.
listener: Connected!
hello

The text 'hello' was typed in the telnet session and appears on the server side 
'handler:hello'.  This means that the handler thread can read from the glob.

The listener thread can write to the glob but the handler appears not to be 
able to.

Is there a solution? 


very best regards,
William D. Ward, Jr.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to