From: Bill Kelly Date: 2009-07-09T01:46:33+09:00 Subject: Re: How to accept a varying amount of UDP packets using recvfrom From: "Randy General" > > I'm currently creating an application that can query a game-server for > its status, total players, hostname, etc. I send out a challenge packet, > and in return I receive between 1 and 3 UDP packets. > > Is there an effective way to deal with receiving a varying amount of UDP > packets? If I do a recvfrom for two packets, the process will time out > if only a single packet arrives. Generally, use select to specifiy the desired timeout. However, it's a little more complicated with ruby and UDP, such that one may end up needing to wrap the recvfrom in a timeout { } block anyway. (On some operating systems, if the UDP packet has a checksum error, select can return ready, but recvfrom will block. (And in many (all?) versions of ruby, setting fcntl O_NONBLOCK does not help due to ruby's internals.)) This rubyforge project has code for receiving one-or-more UDP packets from a Quake2 server: http://rubyforge.org/scm/?group_id=288 Unfortunately, it doesn't seem possible to browse the CVS repository via the web anymore, but you can still check out the source via anonymous CVS as described on that page. E.g. cvs -d:pserver:anonymous@rubyforge.org:/var/cvs/dorkbuster login cvs -z3 -d:pserver:anonymous@rubyforge.org:/var/cvs/dorkbuster co dorkbuster The relevant source file is: dorkbuster/dbcore/q2rcon.rb Here's the innermost receive method: http://pastebin.com/f716b381d Hope this helps, Bill