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

Hi Monks, I have a question about sockets. I have a tcp/ip client that is using non-blocking socket object and IO::Select object to read and write when it is good time for it. I was wandering what would an ideal timeout time for can_read() and can_write() be. I would like to report immediately when the connection is bad. I am doing the following at the moment:

if($socket->can_read(0.3)) { readstuff ... } else { raise error... }

The environment where I will be using this client is Wireless 11mbs network.

I noticed that some times although the remote host is not offline - the 0.3 timeout looks too little.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: IO::Select can_read/can_write timeout question
by ikegami (Patriarch) on Nov 16, 2006 at 15:26 UTC
    If you can both read and write from the socket, I wouldn't use can_read and can_write at all. I'd use IO::Select::select($sel, $sel, undef, $timeout); I'm assuming you close the connection or "ping" the other end on timeout. If not, you can simply use IO::Select::select($sel, $sel);
Re: IO::Select can_read/can_write timeout question
by salva (Canon) on Nov 16, 2006 at 17:12 UTC
    Set the SO_KEEPALIVE option on your socket and let your OS do the hard work for you!
    use Socket qw(SOL_SOCKET SO_KEEPALIVE); setsockopt($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
Re: IO::Select can_read/can_write timeout question
by kabeldag (Hermit) on Nov 22, 2006 at 13:32 UTC