cee flee <> wrote:
> Hello,
>
> I have a problem using a threaded TCP-server. Clients connect to send
> a short message. This message is then processed. This processing can
> take some time (seconds). Writing such a server for Linux is easy,
> but Windows is different. I tried several examples and they all
> failed.
>
> This one keeps working, behaves the same on Linux (Perl 5.8.4) and
> Windows (ActivePerl 5.8.8 816), but generates an error message with
> each
> connect:
> thread failed to start: Undefined subroutine &main::1 called at
> [reference to $thr = threads->new(&process_request($client));]
>
> Does anyone know what is wrong? I don't see how a thread ends.
> (Threading is not my thing, until this week I always forked.)
>
> Thanks,
> momo
>
> ---
> #!/usr/bin/perl
> use strict;
It's a good idea to have "use warnings;" as well.
> use threads;
> use IO::Socket;
> use IO::Select;
>
> my $DONE = 0;
> my $path = './';
> my ($thr);
>
> $SIG{INT} = $SIG{TERM} = sub { $DONE++ };
>
> my $port = 1080;
> my $socket = IO::Socket::INET->new( LocalPort => $port,
> Type => SOCK_STREAM,
> Listen => SOMAXCONN,
> ReuseAddr => 1
> ) or die "Can't create listen
> socket: $!"; my $IN = IO::Select->new($socket);
>
> while (!$DONE) {
> next unless $IN->can_read;
This is effectively a tight loop that will eat CPU. Is that really what
you want?
See IO::Select perhaps.
> next unless my $client = $socket->accept;
> $thr = threads->new(&process_request($client));
Check the documentation for the new/create function. It expects you to
give it some code to execute, either by name or reference. What you are
passing it is the value returned by executing the subroutine. Perhaps
you mean:
$thr = threads->new(\&process_request, $client);
> $thr->detach;
> }
>
> ### process the request
> sub process_request {
> my $client = $_[0];
> local %ENV = ();
> my $line = <$client>;
> #
> #do my thing
> #
> close $client;
> }
HTH
--
Brian Raven
=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the
intended addressee(s). Unauthorised reproduction, disclosure, modification,
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately
and delete it from your system. The views expressed in this message do not
necessarily reflect those of Atos Euronext Market Solutions.
L'information contenue dans cet e-mail est confidentielle et uniquement
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee.
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail
vous parvient par erreur, nous vous prions de bien vouloir prevenir
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre
systeme. Le contenu de ce message electronique ne represente pas necessairement
la position ou le point de vue d'Atos Euronext Market Solutions.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs