How to accept a varying amount of UDP packets using recvfrom

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.

Any assistance is appreciated!

Randy G. wrote:

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.

Any assistance is appreciated!

You need to specify how long to wait until you decide that no more
packets will arrive. Can you guarantee that the packets will arrive
within 500ms? Then make that your timeout.

You can timeout socket reads with Kernel#select(), but you probably knew
that.

From: “Randy G.” [email protected]

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:[email protected]:/var/cvs/dorkbuster login
cvs -z3 -d:pserver:[email protected]:/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

Bill K. wrote:

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

The relevant source file is: dorkbuster/dbcore/q2rcon.rb

Here’s the innermost receive method: http://pastebin.com/f716b381d

Hope this helps,

Bill

Thanks for your replies!

The code in that pastie is a bit beyond my current skill level (just
began programming a few days ago), but I’ll see if I can make some sense
of it and incorporate it into my own app. Thanks for your help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs