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

Hi, I'm trying to write a simple TCP client that tries to keep the TCP connection as long as possible, but reconnects if the connection is broken (say during a server restart). I tried to use the eval/$@ pair to catch the error as in the following, somehow, when the server restarts, it gives me a broken pipe:
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; }
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.

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
    Writing to a closed socket causes a SIGPIPE, which is a signal, not an exception. So you have to handle that somewhow, ignore is usually simplest. Then writing to a closed socket becomes just a normal I/O error. Assuming we don't have to worry about the speed at which I/O progresses, the code becomes:
    #! /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 }
    (warning, only minimally tested)

    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.

Re: Handling server socket error and reconnect
by pg (Canon) on Nov 08, 2004 at 19:09 UTC

    Hopefully this is not your entire code, but if this is, what do you expect $client to be in your eval block for the first time? You have never created the connection, how can you read from or write to it ;-?

    Update:

    By the way, your program has syntax errors...

      Somehow copy/past doesn't work from emacs to netscape on Fedora for me. Updated the syntex error.

      when it starts, $client is undef, so the $@ block is called to establish the connection. I was hoping it will do the same thing if the connection is broken by the server.