It looks like I may have answered my previous question. % cat listener_test #!/usr/bin/perl
use strict; use threads; use IO::Socket; my $port = 11444; my $listener_thread = threads->new(\&listener); $listener_thread->join(); sub listener { my $listening_socket = IO::Socket::INET->new( Listen => 128, LocalAddr => "localhost", LocalPort => $port, Proto => 'tcp' ) or die; printf("listener[%s]: Waiting for connections on port $port.\n", $listening_socket->fileno()); while ( my $client_socket = $listening_socket->accept() ) { printf("listener[%d]: New incoming connection.\n", $client_socket->fileno()); threads->new(\&protocol_handler, $client_socket)->detach(); } } sub protocol_handler { my $socket = shift; my $fileno = $socket->fileno(); printf("handler[%d]: Waiting for message.\n", $fileno); $socket->send("> "); while ( my $message = <$socket> ) { $message =~ s/[[:cntrl:]]//g; printf("handler[%d]: >%s<\n", $fileno, $message); if ( $message =~ /^exit/i ) { last; } $socket->send("> "); } $socket->close(); printf("handler[%d]: Client exiting.\n", $fileno); } Server side output. % listener_test listener[4]: Waiting for connections on port 11444. listener[5]: New incoming connection. handler[5]: Waiting for message. listener[6]: New incoming connection. handler[6]: Waiting for message. handler[6]: >list connections< handler[5]: >help< handler[5]: >exit< handler[5]: Client exiting. handler[6]: >exit< handler[6]: Client exiting. ^C ----- Original Message ----- From: William Ward <eusw...@yahoo.com> To: "beginners@perl.org" <beginners@perl.org> Cc: Sent: Thursday, August 2, 2012 5:13 PM Subject: Passing socket descriptor between threads 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/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/