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

I'm pretty new to all things socket related, I need to wrap a timer around a socket's output so it waits for no longer than n seconds then moves to next. As I understand it the Timeout argument for IO::Socket is the actual timeout to connect rather than for receiving data.
I imagine something like this is needed:

#start timer; while (<$socket>) { # check timer # grab a bit of data }
My concern was that some sort of alarm would be needed to interrupt the while loop if no data was forthcoming.. (i've seen reference to a couple of modules here, but would realy prefer to hardcode this rather than using external code).
Any help much appreciated..

Replies are listed 'Best First'.
Re: Timing Socket Output
by pfaut (Priest) on Jan 27, 2003 at 15:12 UTC

    You can use select (perldoc -f select) or IO::Select to check for availability of data on multiple sockets. select() allows for the specification of a timeout. This function will allow you to wait for data to become available on any socket of interest so you don't have to go socket to socket looking for work to do.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Timing Socket Output
by Ryszard (Priest) on Jan 27, 2003 at 15:46 UTC
    How about:
    .. .. $SIG{ALM} = \&timed_out; eval { alarm(5) #socket code here }; if ($@=~/dead/) { #time out handling here } alarm(0); .. .. sub timed_out{ die "dead"; }

    From there you can do whatever handling you like (and should prolly change the 5 to something like $timeoutValue)