#!/usr/bin/perl -w use IO::Socket; use IO::Select; use Time::HiRes; if (!defined($ARGV[0])) { print "usage: client.pl address[:port] continuous nosleep\n"; exit; } ($address,$port)=split(/:/, $ARGV[0]); $port=2655 if !defined($port); $contFlag= defined($ARGV[1]) ? 1 : 0; $sleepFlag=defined($ARGV[2]) ? 0 : 1; $SIG{"INT"}=\&sigInt; # for ^C select STDOUT; $|=1; while(1) { logit("OPEN"); $socket=new IO::Socket::INET( PeerAddr => $address, PeerPort => $port, Proto => 'tcp', Timeout =>1); if (!defined($socket)) { logit("Couldn't connect to server, retrying"); sleep 1; next; } $select = new IO::Select($socket); logit("Try to read"); while (my @ready=$select->can_read()) { logit("Can_read"); $bytes=sysread($socket, $line, 100); if ($bytes==0) { logit("Socket closed on other end"); $socket=''; last; } @handles=($select->can_read(0)); last if scalar(@handles)==0; } chomp $line; logit($line); logit("client close"); $socket->close if $socket ne ''; $select->remove($socket); last if !$contFlag; sleep 1 if $sleepFlag; } sub sigInt { print "Close Socket\n"; $socket->close(); exit; } sub logit { my $text=shift; my ($intSeconds, $intUsecs)=Time::HiRes::gettimeofday(); $time=sprintf("$intSeconds.%06d", $intUsecs); print "$time $text\n"; }