Jose,

Yes, you are correct. I did not know about placing the alarm prior to the hanging condition (gee, I guess that makes sense) or clearing the alarm afterwards. Thank you!
-Ian

NYIMI Jose (BMB) wrote:

-----Original Message-----
From: Ian Zapczynski [mailto:[EMAIL PROTECTED]] Sent: Friday, December 20, 2002 7:01 PM
To: [EMAIL PROTECTED]
Subject: how to use alarm() to properly time out a connection?


Hello all,

I need to use alarm to time out a connection to an SFTP server if the connection hangs (I was able to reproduce such a hang using the Secure Shell Server for Windows, so I thought it would be nice if my script assumed this was a possibility).

I think I am not understanding the perldoc for alarm properly, because the following test script did not do what I want it to do:

#!/usr/bin/perl -w

use strict;

eval {
while (1) {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 60;
}
};

if ($@) {
print "uh, I didn't time out" unless $@ eq "alarm\n"; }

In reality, I don't want such a loop that is always true - this is just my test case. The real code looks more like:

eval {
$sftp = Net::SFTP->new($FTPHOST, user=>$FTPUSER, password=>$FTPPASS);
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 60;
};

Hum, i'm wondering if alarm 60 should not be before your sftp connection ...:
As far as I know, the sequence is:
-set alarm (alarm 60 )
-perform a long time operation ...
-clear alarm (alarm 0)

So i think your code should be something like this:

eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 60;
$sftp = Net::SFTP->new($FTPHOST, user=>$FTPUSER,password=>$FTPPASS);
alarm 0;
};
if ...


HTH

José.



if ($@) {
&sendSNMP unless $@ eq "alarm\n"; }

When I run the test code with the endless while loop, the script never times out. Can someone explain how I miswrote my script?

Unfortunately the hung SFTP connection is hard to reproduce consistently, so I want to be sure my code *should* work if such a situation occurs. Please note that the SFTP connection must exist in an eval() so when it dies, it does not exit my script.
Thanks!



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




**** DISCLAIMER ****

"This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent.



Reply via email to