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

Greetings,

I have a program that accepts socket connections with IO::Socket (or rather its subclass HTTP::Daemon). Because this program is also checking for user input, I'm using a timeout on the accept call. But it appears that the minimum timeout is 1 second. In the interest of responsiveness to picky users, I'd like to make it shorter if I could (maybe 0.25 or 0.5 sec). Is there a way to do this, or some creative workaround I could employ instead? My current code looks roughly like:

my $d = HTTP::Daemon->new ( LocalAddr => 'localhost', LocalPort => 1244 ) or die; $d->timeout(1); MAIN_LOOP: while (1) { my $c = $d->accept; unless (defined $c) { do_other_stuff(); next MAIN_LOOP; } process_request($c); }
I'm running on Win32 ActivePerl 5.8.6 in case it matters.

Update: I am on crack. saintmike is correct. I don't know why I thought otherwise (or why I didn't retest one last time). saintmike++.

Replies are listed 'Best First'.
Re: short (<1sec) timeout on IO::Socket
by saintmike (Vicar) on Aug 29, 2005 at 22:14 UTC
    Specifying a fraction of a second should work just fine, since IO::Socket uses IO::Select, which uses perl's select which accepts values like 0.25.
      I thought I tried that and it didn't work (I thought it truncated it to zero and did a non-blocking call). I'll try again.

      IO::Socket uses IO::Select? You may want to re-evaluate this. Socket was never based on selector. If one is from c background, will right the way find this false.

      Update: saintmike was right and I was wrong. Perl's IO::Socket does require IO::Select at two places: accept and connect.

      I tried this code and it returned after 1 second as expected:

      use IO::Socket; use strict; use warnings; print time(), "\n"; my $socket = IO::Socket::INET->new(Proto => "tcp", LocalAddr => "local +host", LocalPort => 3000, Listen => 10, Timeout => 0.1); for (1 .. 10) { my $connection = $socket->accept(); } print time(), "\n";