In article <495611ef806a1_87e7c49f58106bb / redmine.ruby-lang.org>,
  Oleg Puchinin <redmine / ruby-lang.org> writes:

> Feature #936: IO.nread function
> http://redmine.ruby-lang.org/issues/show/936

>      p s2.recvfrom_nonblock(s2.nread)  #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]

io/wait library provides IO#ready?.

% ./ruby -rsocket -rio/wait -e '
s1 = UDPSocket.new
s1.bind("127.0.0.1", 0)
s2 = UDPSocket.new
s2.bind("127.0.0.1", 0)

s2.connect(*s1.addr.values_at(3,1))
s1.connect(*s2.addr.values_at(3,1))
s1.send "aasdfsadfwasdfasdaa", 0
IO.select([s2]) # emulate blocking recvfrom

p s2.ready?
p s2.recvfrom_nonblock(s2.ready?)
'
19
["aasdfsadfwasdfasdaa", ["AF_INET", 34732, "localhost", "127.0.0.1"]]

Another idea is to make the max argument of
UDPSocket#recvfrom_nonblock's optional.

Since no UDP packet longer than 65536 bytes, 65536 is a safe
default value for max of UDPSocket#recvfrom_nonblock.
-- 
Tanaka Akira