Ivo Palli
1/25/2005 5:38:00 PM
You are correct, sysread() returns without blocking. However this is a
workaround at best. In the interest of portability over platforms, I
still recommend for fcntl() to be implemented in the Win32 platform.
Afterall Ruby aims to provide a unified platform regardless of OS. Right?
Are there any ways to detect the OS btw? If I'm going to run my script
on multiple platforms, and I need to workaround, how can I do this
automatically?
Regards,
Ivo Palli
Tanaka Akira wrote:
> In article <41F65F1B.80706@palli.nl>,
> Ivo Palli <ivo@palli.nl> writes:
>
>
>>Instead I want to use a select, which is thankfully support in Ruby.
>>However a read to a socket which has data open always seems to block,
>>unless I specifically read what is available. Since I cannot know how
>>much data is waiting for me, I really need to do a non-blocking read. In
>>Linux it works, in Windows the fcntl call is not supported. :( As far as
>>I searched and tried, there is no other way to read data without blocking.
>
>
> sysread might be usable because sysread doesn't block when some data
> available. If sysread is usable, you don't need non-blocking read.
>
> require "socket"
>
> gs = TCPserver.open(0)
> addr = gs.addr
> addr.shift
> printf("server is on %s\n", addr.join(":"))
> socks = [gs]
>
> loop do
> nsock = select(socks);
> next if nsock == nil
> for s in nsock[0]
> if s == gs
> ns = s.accept
> socks.push(ns)
> print(s, " is accepted\n")
> else
> begin
> print "[" + s.sysread(4096) + "]\n"
> rescue EOFError
> print(s, " is gone\n")
> s.close
> socks.delete(s)
> end
> end
> end
> end