#!/usr/bin/perl $|++; use strict; use warnings; our $DEBUG = 1; my $query = $ARGV[0] || 'default data'; print client($query); sub client { use IO::Socket; # <- here for when this sub grows up to become an object method # do some basic setup stuff my $info = shift; return "No info!" unless ($info); my @ips = ('XXX.XXX.XXX.X0','XXX.XXX.XXX.X1','XXX.XXX.XXX.X2'); my $port = XXXX; # grab an IP randomly my $index = int(rand(scalar @ips)); # try to connect once for each IP my $sock; for (my $counter = 0; $counter < scalar @ips; $counter++) { # debug output print "Trying $ips[$index]... " if ($DEBUG); # try to connect $sock = IO::Socket::INET->new( PeerAddr => $ips[$index].':'.$port, Blocking => 1, Timeout => 1, ### The key param that I had been missing!!! ); # success! if ($sock) { print "OK!\n" if ($DEBUG); last; } # failure print "Nope\n" if ($DEBUG); # get previous IP (loops around if negative, pretty sweet) $index--; } # return error if all 3 failed return "no sock!" unless ($sock); # write to socket print $sock $info.$/ or die "$!"; # read from socket and close sysread $sock, my ($text), 10000; close($sock); return $text; }