Link to home
Start Free TrialLog in
Avatar of Ivanov_G
Ivanov_GFlag for Bulgaria

asked on

Problem: using perl IO::Socket::INET and $SIG{ALRM}

Is it possible to use $SIG{ALRM} together with IO::Socket::INET tcp server? Seems that triggered alarm functions cause accept to end with error, for example:

#!/usr/bin/perl

use IO::Socket::INET;


my %sock_parm;
$sock_parm{Type}      = SOCK_STREAM;
$sock_parm{Proto}     = "tcp";
$sock_parm{LocalPort} = 8080;
$sock_parm{Listen}    = SOMAXCONN;
$sock_parm{Reuse}     = 1;

my $sock = new IO::Socket::INET(%sock_parm);


sub tick {
  print "Tick!\n";
  alarm(5);
}

$SIG{ALRM} = \&tick;
alarm(5);


print "Waiting...\n";
my $client = $sock->accept() || print "Failure!\n";
print "Done.\n";
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Hi,

You might want to post your question at
https://www.experts-exchange.com/Programming/Programming_Languages/Perl/
which is Perl specific.

Wesly
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ivanov_G

ASKER

Hi ozo,
thanks for your reply, the solution you suggest seems to work OK. Anyway, accept still gets interrupded:

use POSIX;
my $client;
print "." until $client = $sock->accept() || ($! != EINTR&&print "Failure! $!\n");

Do you know the actual reason why $SIG{ALRM} interrupts accept?
I think this is not perl related problem and similar program in C++ for example will most probably behave exactly the same way?
C will behave the same way.

     4 EINTR Interrupted function call.  An asynchronous signal (such as
             SIGINT or SIGQUIT) was caught by the process during the execution
             of an interruptible function. If the signal handler performs a
             normal return, the interrupted function call will seem to have
             returned the error condition.