Is it possible to impose a timeout on <>?

B

Bob Dubery

Hi all

I have code that polls a number of machines on our network and
retrieves stats from each one.

This is done via HTTP

This morning the program hung. When I started investigating I found
that one of the remote machines was in some kind of strange state. You
could ping it. You coud connect to it (HTTP, Telnet, FTP) but you got
no response.

So my program opens up a socket, fires off the HTTP request and then
waits for a response from apache, and waits, and waits, and waits...

The relevant portion of the code looks like this...

# open a socket...
$socket = IO::Socket::INET->new(PeerAddr => $netaddr,
PeerPort => 80,
Proto => "tcp",
Timeout => 10)
or die "Couldn\'t open the socket!!!!";

# form the HTTP request
$request = "GET $url HTTP\/1.0\n\n";

# request the file...
print $socket $request;

# process the output and strip out the response headers
$printit = 0;
# ...now read the socket and process the output
while($line = <$socket>){

# do some stuff with $line
}

The program hangs at the <> operator.

OK... a wierd situation and not a common one, but I want to improve
the code. Specificially when I invoke <> I'd like to be able to impose
a time out so that if I get nothing back after n seconds I can raise
an error condition and start interrogating the next site.

TIA

Bob Dubery


PS... I have to post via Google, which is slow, so I might take a
while to see and respond to any questions.
 
A

Andrew McGregor

Bob Dubery said:
OK... a wierd situation and not a common one, but I want to improve
the code. Specificially when I invoke <> I'd like to be able to impose
a time out so that if I get nothing back after n seconds I can raise
an error condition and start interrogating the next site.

perldoc -f alarm
 
P

pkent

This is done via HTTP ....
The program hangs at the <> operator.

OK... a wierd situation and not a common one, but I want to improve
the code. Specificially when I invoke <> I'd like to be able to impose
a time out so that if I get nothing back after n seconds I can raise
an error condition and start interrogating the next site.

Here is some example code:

my $line;
eval {
alarm($alarmtime);
$line = <SOCKET>;
};
alarm 0; # cancel the alarm
if ($@) {
# whatever error handling...
}

There may be better ways that use lower-level reading functions though.

P
 
B

Bob Dubery

pkent said:
Here is some example code:

my $line;
eval {
alarm($alarmtime);
$line = <SOCKET>;
};
alarm 0; # cancel the alarm
if ($@) {
# whatever error handling...
}

There may be better ways that use lower-level reading functions though.
Many thanks. I'll give this a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top