johnnywang has asked for the wisdom of the Perl Monks concerning the following question:
Also how do I handle the case when the server is really down, i.e., the "die" part, I'd like to return an "undef" rather than a "die". Thanks.use strict; use IO::Socket; my $client; foreach(1..10){ print call_server(),"\n"; sleep 10; # allow sometime to test server restart } sub call_server{ my $answer; eval{ print $client "some_command\n"; $answer = <$client>; }; if($@){ $client = new IO::Socket::INET(PeerAddr => 'localhost', PeerPort => 16666, Proto => 'tcp', Type => SOCK_STREAM) or die "Couldn't connect to + server: $!"; # try again print $client "some_command\n"; $answer = <$client>; } return $answer; }
Updated: syntex errors.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Handling server socket error and reconnect
by thospel (Hermit) on Nov 09, 2004 at 01:31 UTC | |
|
Re: Handling server socket error and reconnect
by pg (Canon) on Nov 08, 2004 at 19:09 UTC | |
by johnnywang (Priest) on Nov 08, 2004 at 19:44 UTC |