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

i am running the following code under perl 5.8.4 on a win 2000 server. what it does is this: information is passed to this routine and then is sent to 1 of 2 possible locations. the code should try to send to 1 then if the connection fails try the other one and continue to do that for a maximum of 2 times per ip. The issue i am running into is the code fails to flip-flop. If the connection fails or nothing comes back after 5 seconds i need to switch the connection to the other system. any suggestions on alternative ways to handle this task and/or any ideas why my code fails to do this properly would be a great help. thanks ahead of time for all the great wisdom that resides behind this url.
sub send_to_processor{ my $message = $_[0]; use Sys::Hostname; use Socket; my @transaction_ips = ('10.30', '10.31'); #If connection isn't open, then open it for(my $i = 0;$i < 4;$i++){ last if($remote = IO::Socket::INET->new(Proto => 'tcp', PeerAd +dr => $transaction_ips[($i%2)], PeerPort => 8120, Timeout => 5)); } #If connection still isn't open, #then connection is unavailable unless(defined $remote){ return(0, 'Server Unavailable!'); } #Send Message print $remote $message; #Recieve Response my $response; recv($remote, $response, 2048, 0); #Close Connection close $remote; return (1,$response); }

Replies are listed 'Best First'.
Re: flip-flop socket locations if 1 is down
by monkey_boy (Priest) on Jun 09, 2005 at 16:51 UTC
    Try changing:
    last if($remote = IO::Socket::INET->new(Proto => 'tcp', PeerAddr => $ +transaction_ips[($i%2)], PeerPort => 8120, Timeout => 5));
    to:
    $remote = IO::Socket::INET->new(Proto => 'tcp', PeerAddr => $transacti +on_ips[($i%2)], PeerPort => 8120, Timeout => 5)); last if $remote;



    This is not a Signature...
      thank you for your suggestion. can i ask what is the reason that you suggested this change?
      is the first way not doing the same thing by checking the result of the assignment.