Home: Perl Programming Help: Intermediate:
Win32 and alarm()



alager
Novice

Sep 3, 2009, 3:57 PM


Views: 1223
Win32 and alarm()

I'm making a UDP test script and the receive portion uses alarm() to test for timeouts. The problem is that calling the recv() method of IO::Socket seems to cause the alarm feature to either be reset, or just not able to create the signal.

alarm() by itself works:

Code
$SIG{ALRM} = sub { die "alarm time out" }; 
alarm(1);
sleep 4;
alarm(0);

This gives the message "alarm time out"

However, if I try this instead, the code hangs forever if there is nothing to receive, the alarm never activates

Code
$SIG{ALRM} = sub { die "alarm time out" }; 
alarm($TIMEOUT);
$sock->recv($msgRX, $MAXLEN) or die "recv: $!";
alarm(0);


I've tried this with and without an eval around it, it doesn't seem to make a difference.

Any help on this would be appreciated.

Thanks,
Aaron


(This post was edited by alager on Sep 4, 2009, 9:06 AM)


1arryb
User

Sep 8, 2009, 8:09 AM


Views: 1196
Re: [alager] Win32 and alarm()

Hi alager,

The fact that your alarm is being reset inside the socket call should have twigged you to the need to deal with the timeout in your socket setup, not the surrounding Perl code. Looking at the UDP example code in the perlipc perldoc, it looks like you should set the socket timeout in your select before attempting the recv call.

Good Luck,

Larry