Imposing a timeout on the <> operator - is this possible?

Discussion in 'Perl' started by Bob Dubery, Mar 5, 2004.

  1. Bob Dubery

    Bob Dubery Guest

    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
    Bob Dubery, Mar 5, 2004
    #1
    1. Advertising

  2. Bob Dubery

    JSM Guest

    (Bob Dubery) wrote in message news:<>...

    > 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.
    >


    You can use alarm to do this. There is an example in the Perl Cookbook (16.21).
    JSM, Mar 5, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,724
    Bob Johnson
    Aug 7, 2003
  2. Do
    Replies:
    2
    Views:
    6,327
  3. Mel
    Replies:
    1
    Views:
    563
  4. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,251
    Brian Candler
    Oct 6, 2004
  5. Anselm
    Replies:
    0
    Views:
    81
    Anselm
    May 30, 2006
Loading...

Share This Page