How to accept a varying amount of UDP packets using recvfrom

R

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.

Any assistance is appreciated!
 
J

Joel VanderWerf

Randy said:
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.
 
B

Bill Kelly

From: "Randy General said:
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
 
R

Randy General

Bill said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top