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";
LVL 12
Ivanov_GAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wesly_chenCommented:
Hi,

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

Wesly
0
ozoCommented:
my $client;
1 until $client = $sock->accept() || ($!ne"Interrupted system call"&&print "Fai\
lure! $!\n");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ivanov_GAuthor Commented:
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?
0
ozoCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.