in reply to Handling server socket error and reconnect
(warning, only minimally tested)#! /usr/bin/perl -w use strict; use IO::Socket::INET; $SIG{PIPE} = "IGNORE"; my $client; sub call_server { my $answer; for (0..1) { return $answer if $client and print $client "some_command\n" and defined($answer = <$client>); $client = IO::Socket::INET->new(PeerAddr => "localhost", PeerPort => 16666) or die "Couldn't connect to server: $!"; } die "Server in trouble"; } foreach(1..10) { print call_server(); sleep 10; # allow sometime to test server restart }
updated: code didn't take into account that a write might still get out, and only the read will notice the close. Also be more paranoid about a bad server, only retry once and assume an immediate second failure is a sign of server problems.
updated: make the looping on first call consistent too.
|
|---|