Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a script that works, but I'm trying to add some additional features to it. Right now it will go through the sub check and see if a server is listening for a particular service on two interfaces (it's dual homed). If it is not, then it will run another sub that performs some additional commands. I want to take into account network delay, so if it does notice an interface down initially, I would like for the script to go through the check again before running the run_fix sub. I'm not sure what the best way of doing this without causing an infinite loop. Maybe using redo? I would appreciate any suggestions.
#!/usr/bin/perl -w use strict; use IO::Socket::INET; my @host = ('192.168.149.3', '192.168.15.3'); my $port = '80'; my $connected; check(); sub check { for (@host){ my $sock = IO::Socket::INET->new(PeerAddr => $_, PeerPort => $port, Proto => 'tcp', timeout => '10') and $connected = 1; if ($connected) { print "$_ is listening on port $port.\n"; } else { print "$_ is NOT listening on port $port proceeding to failover.\n" + && run_fix(); } } }
Thank You

Replies are listed 'Best First'.
Re: Trying Not To Cause an Infinite Loop
by pg (Canon) on Dec 09, 2002 at 15:58 UTC
    There is something more serious than this infinite loop concern.

    I am wondering how did you determine whether a pair of client/server still connected? Although you have a $connected, but that variable was only set once after the connection was established, and after that there was no check any more. Is this $connected meaningful? No, not at all, the connection can be half way shut down by either peer, but $connected is always TRUE.

    IO::Socket::INET has a method called ->connected, can you use that one to check, again, it is a NO. That variable is only set once after the connection is established, there is no checking afterwards, unless locally close the socket. (play with the attached code, it demos this.)

    In order to detect whether the connection is alive, you have to define your own "heart beat" mechanism, also define the maximum amount of heart beat lost you can tolerate, after that just take it as a dead connection.
    server.pl: use IO::Socket::INET; $server = new IO::Socket::INET(Timeout => 20, Proto => "tcp", LocalPor +t => 3000, Listen => 5) || die "failed to establish socket\n"; $client = $server->accept; while (1) { sleep(2); if ($client->connected) { print "still connected\n"; } else { print "disconnected\n"; } } client.pl: use IO::Socket::INET; $client = new IO::Socket::INET(Timeout => 20, Proto => "tcp", PeerPort + => 3000, PeerAddr => "localhost") || die "connection failed"; while (1) { sleep(2); if ($client->connected) { print "still connected\n"; } else { print "disconnected\n"; } }
Re: Trying Not To Cause an Infinite Loop
by Tanalis (Curate) on Dec 09, 2002 at 15:21 UTC
    Heys,

    There are a couple ways you could do this - what I'd probably do is set a constant $MAX_RETRIES and then either locally decrement this, or count a loopcounter up to this, using last if a connection is made.

    For example: (untested code)

    my $MAX_RETRIES = 3; foreach (@host) { foreach $attempt ( 1 .. $MAX_RETRIES ) { my $sock = IO::Socket::INET->new(PeerAddr => $_, PeerPort => $port, Proto => 'tcp', timeout => '10') and $connected = 1; last if $connected; } unless $connected print "$_ is NOT connected.\n" && run_fix(); }

    You could extend this to have the script sleep for a set number of seconds before retries, which is maybe a good idea if there's lag involved on the network.

    Just some quick ideas ..
    Hope that helps.
    -- Foxcub