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

Posted on 2004-12-01
Last Modified: 2012-06-27
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:


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";

$SIG{ALRM} = \&tick;

print "Waiting...\n";
my $client = $sock->accept() || print "Failure!\n";
print "Done.\n";
Question by:Ivanov_G
    LVL 38

    Expert Comment


    You might want to post your question at
    which is Perl specific.

    LVL 84

    Accepted Solution

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

    Author Comment

    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?
    LVL 84

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now