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

I'm trying to read of a socket and run a command every time it loops, which should be every second. It should check for data on the Socket and then run this sub(timer();). Here is what I have so far.
print "Opening connection to $config{server} on port $config{port} ... +\n"; $sock = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $config{server}, PeerPort => $config{port} ) or print "Unable to connect: $!\n"; $sock->autoflush(1); print STDERR "Connected to $config{server}:$config{port}\n"; my $readers = IO::Select->new() or die "Can't create IO::Select read o +bject"; $readers->add($sock); $on = $TRUE; while ($on) { if(!defined $timeout) { $timeout = 0; } my @ready = $readers->can_read; for my $handle (@ready) { if($handle eq $sock) { $bytes = sysread($sock, $buffer, 4096); print "buffer: $buffer\n"; if($bytes > 0) { $botstats{bytes_rcvd} += $bytes; } while (defined ($line = <$sock>)) { $line =~ s/[\012\015]+$//; next unless $line; handle_input(\%active_channels, $line); timer(); } } }
I'm sure this is something simple, just a misplace of the timer(); thanks

Replies are listed 'Best First'.
Re: While? and IO::Socket
by steves (Curate) on Feb 15, 2003 at 09:17 UTC

    It looks to me like your logic is a bit wrong. You're doing a sysread to read a chunk off the socket, then are immediately trying to do line reads off the same socket with the while loop. Since the timer() call is in the line reading loop, I'm guessing you're never getting in there because the socket is empty, having had all its data read with the previous sysread.

Re: While? and IO::Socket
by so14k (Initiate) on Feb 15, 2003 at 06:12 UTC
    btw.. this is mine, so please send all responses to me.