I have a code where I send command to another machine and wait for it's response over UDP. Now, I want when the response from remote machine stops coming for 'time-out' number of seconds i should close the socket and come out. Here is the code/function i am using. Problem is alarm is not triggered and the program remains in infinite loop. Can any one help.
sub ReceiveResponse { my $port = shift; my $time_out = 1; print "FW Machine: Waiting for Command Response On Port $recv_port +\n"; eval { local $SIG{ALRM} = sub { die "Timed Out"; }; my $socket_resp = IO::Socket::INET->new(LocalPort => $recv_por +t, Proto => 'udp', Timeout => undef); alarm 10; while (1) { my $recieved_data; $socket_resp->recv($recieved_data,1024); my $peer_address = $socket_resp->peerhost(); my $peer_port = $socket_resp->peerport(); if ($peer_address eq $send_ip_address) { my $desti = GetIP(); chomp($recieved_data); print "$peer_address:$peer_port > $desti:$recv_port => + $recieved_data\n"; if ($recieved_data =~ m/^done/i) { last; } } } $socket_resp->close(); alarm 0; }; alarm 0; if ($@ =~ /Timed Out/i) { print "Timed-Out Receiving Data from UDP\n"; } }

In reply to alarm not working by anshumangoyal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.