aSocket.select

Dear All,

I can’t find how to use aSocket.select with a timeout. Can u tell me how?

···


Yours truly, WBR, Paul Argentoff.

I can't find how to use aSocket.select with a timeout. Can u tell me how?

svg% ri Kernel::select
--------------------------------------------------------- Kernel::select
     select( readArray [, writeArray [errorArray [timeout]]] ) ->
     anArray or nil

···

------------------------------------------------------------------------
     Performs a low-level select call, which waits for data to become
     available from input/output devices. The first three parameters are
     arrays of IO objects or nil. The last is a timeout in seconds,
     which should be an Integer or a Float. The call waits for data to
     become available for any of the IO objects in readArray, for
     buffers to have cleared sufficiently to enable writing to any of
     the devices in writeArray, or for an error to occur on the devices
     in errorArray. If one or more of these conditions are met, the call
     returns a three-element array containing arrays of the IO objects
     that were ready. Otherwise, if there is no change in status for
     timeout seconds, the call returns nil. If all parameters are nil,
     the current thread sleeps forever.
        select( [$stdin], nil, nil, 1.5 ) #=> [[#<IO:0x4019202c>], , ]

svg%

You have an example in lib/net/telnet.rb

      until(prompt === line and not IO::select([@sock], nil, nil, waittime))
        unless IO::select([@sock], nil, nil, time_out)
          raise TimeoutError, "timed-out; wait for the next data"
        end

Guy Decoux

This is obvious, and I wouldn’t ask the question if I was glad with this
pattern. My question was: if I use select as a method of aSocket object (anIO
inherently, which is documented), how can I specify a timeout then. The
interpreter says that anObject.select must be used with 0 arguments, but that
seems to be stupid… – why do I need select if I cannot specify he
request’s time boundary?

···

On Monday 28 July 2003 20:44, ts wrote:

You have an example in lib/net/telnet.rb

  until(prompt === line and not IO::select([@sock], nil, nil,

waittime)) unless IO::select([@sock], nil, nil, time_out)
raise TimeoutError, “timed-out; wait for the next data”
end


Yours truly, WBR, Paul Argentoff.

This is obvious, and I wouldn't ask the question if I was glad with this
pattern. My question was: if I use select as a method of aSocket object (anIO
inherently, which is documented), how can I specify a timeout then. The
interpreter says that anObject.select must be used with 0 arguments, but that
seems to be stupid... -- why do I need select if I cannot specify he
request's time boundary?

You make the confusion between Kernel::select and Enumerable#select

svg% ruby -rsocket -e 'p TCPsocket.new("localhost", 25).select(1)'
-e:1:in `select': wrong number of arguments(1 for 0) (ArgumentError)
        from -e:1
svg%

svg% ruby -rsocket -e 'p TCPsocket.new("localhost", 25).select'
-e:1:in `select': no block given (LocalJumpError)
        from -e:1:in `each'
        from -e:1:in `select'
        from -e:1
svg%

You must use IO::select to specify a timeout

Guy Decoux