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