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";
}
}
| [reply] [d/l] |
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
| [reply] [d/l] [select] |