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

Hello Gurus,

I have not done much socket programming and here I get a script that I have to modify and don't know how. I tried searching this site and a book, but to no avail. Please help (grin).

The script reads lines from a socket using:

while (<$sock>) { ... }
The lines can be grouped by some criteria and when the first line from a group arrives I know how many more lines to expect. Now, if the connection is lazy or someone forgot to send some lines from the group, I should be able to discover it after let's say 5 minutes, log an error and continue with reading from the socket. Any ideas will be gladly received...

topsik

Edit by dws for tag cleanup

Replies are listed 'Best First'.
Re: Reading from socket with timeout (sort of)
by perlplexer (Hermit) on May 09, 2002 at 17:22 UTC
    You can use IO::Select::can_read() to do what you want.
    use IO::Select; # code to initialize $sock my $sel = new IO::Select( $sock ); while (1){ if ($sel->can_read(5 * 60)){ # see if $sock has data. # wait no longer than 5 min while (<$sock>){ # read however many lines you expect } }else{ # log error } } #end while
    A better approach would be to write generic functions for reading and writing from/to a given socket with timeouts but I'll leave that as an exercise for you. :)

    --perlplexer