aSocket.select

P

Paul Argentoff

Dear All,

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

ts

P> 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
 
P

Paul Argentoff

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

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?
 
T

ts

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top