CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Alarm question


New User

Feb 26, 2011, 2:27 AM

Post #1 of 1 (582 views)
Alarm question Can't Post

I'm trying to make a server that reads 2 strings from a socket and returns to the client some processed data.

The thing is $SIG{ALRM} is not overwritten locally in eval and I still get "Alarm clock" and my server exits.

Is it because of the threads I use or am I missing something?
Btw I use Linux.

use strict; 
use IO::Socket;
use threads;
use warnings;

print "Enter port: ";
chomp(my $port = <STDIN>);

my $socket = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => $port,
Proto => 'tcp',
Reuse => 1,
Listen => 5,
or die "Error $!\n";

print "Server $0 is now active.\n";

while (1) {
my $new_client = $socket->accept();
threads->new(\&client_sub, $new_client);

sub client_sub {
my ($client) = @_;
my $buffer = undef;

eval {

local $SIG{ALRM} = sub { die "alarm\n" };
alarm 4;

chomp(my $first = <$client>);
print $first;
chomp(my $second = <$client>);
print $second;

alarm 0;

# some text processing, final string ends in $_

print $client $_;


if ($@) {
print $client "Timeout.\n" if $@ eq "alarm\n";
} else {
die "DIED $!\n";

close $client;


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives