From: Leslie Viljoen Date: 2008-06-11T07:39:32+09:00 Subject: Re: Non blocking UDP On Wed, Jun 11, 2008 at 12:16 AM, Joel VanderWerf wrote: > Leslie Viljoen wrote: >> >> Hello! >> >> I need to make a UDP server that waits for clients to log on and then >> drives >> communication from the server side. This means that if a client doesn't >> ack >> subsequent comms from the server, the server needs to resend those >> comms a few times before giving up. >> >> I have gotten as far as a successful UDP server, but it blocks the thread >> forever, and having a server constantly breaking out of >> socket.recvfrom using Timeout >> does not seem to work at all (port never seems open). I need a UDP wait >> with a timeout. >> >> I have looked at several UDP examples, and tried the sparse docs here: >> http://ruby-doc.org/stdlib/libdoc/socket/rdoc/classes/Socket.html#M004528 >> >> - but no luck yet. I tried recvfrom_nonblock as a guess but it seems to >> block anyway. >> >> Any tips? >> >> Les > > IO#select ? Below is my guess that doesn't work - I get connection refused when trying to connect to the port. Is there any proper documentation for these esoteric methods? http://www.ruby-doc.org/core/classes/Kernel.html#M001109 doesn't help, and even the Pickaxe ed. 3 gives not much more clue than what I tried. #!/usr/bin/ruby -w require 'socket' class UdpServer def initialize(ip, port) socket = UDPSocket.new socket.bind(ip, port) loop do a = IO.select([socket], nil, nil, 5) p a sleep(1) end end end s = UdpServer.new("0.0.0.0", 7778)