Java: Receiving multiple UDP packets

By MrGaribaldi ยท 6 replies
Oct 9, 2004
  1. I've got a little Java problem I was hoping to get some help with.

    I'm starting to dabble (on my own) in java networking over TCP/IP (creating a game browser) and have run up against a little problem.
    When I query a BF1942 server all the info I need is returned in one UDP packet, but if I try to do the same with a UT2k4, I can get one or more UDP packets. And there is no info in the first packet that says how many packets there are in total (though the last packet will end with "\final").

    And since Datagram Packet blocks until a datagram is received I don't want to keep it listening for too long either.

    Basicly what I want to know is if there are any easy ways to deal with receiving an unkown quantity of UDP packages?
    (The book I'm using is for Java 1.3, so I'm hoping there's been some changes since that)

    TIA :)
  2. Nodsu

    Nodsu TS Rookie Posts: 5,837   +6

    Use a separate thread for Datagram Packet maybe so it doesn't block? Are you sure there is no timeout feature in Datagram Packet?

    As for algorithms.. Maybe you can find some open source program where you can see how it is done?
  3. MrGaribaldi

    MrGaribaldi TechSpot Ambassador Topic Starter Posts: 2,512

    Yeah, I'll have to use several threads for receiving.

    There is a time-out feature that I can (and do) use, but since I don't know how many packets I'm going to receive I'll incur a somewhat long waiting time before I'm sure that no more packets are incoming.

    Guess I'll have to try to find out what the max nr of packets the different servers will send out and assume each server sends out that amount of data, and take it from there..

    I'll have a look-see for an Open Source program, good idea :)
  4. Nodsu

    Nodsu TS Rookie Posts: 5,837   +6

    How about receiving one packet at a time?

    You set a short timeout for receiving one packet. If you get the packet and it's not the final one, loop.
  5. MrGaribaldi

    MrGaribaldi TechSpot Ambassador Topic Starter Posts: 2,512

    Sorry, I guess I've not been overly clear.
                DatagramPacket responsePakke = new DatagramPacket(responseData, responseData.length);
    Which is my basic code.

    Now this I can modify to either
    up to the max amount of packets the server can return or
                StringTokenizer st = new StringTokenizer(<serverResponse>);
    Only problem is that I'm afraid I might loose a UDP packet whilst checking if the packet I have received is the last one (as they are supposed to disappear without a word to either sender or receiver)... So I have to catch them fast enough...

    But that last code snippet might very well work (after I've fixed the errors ;)), I just wrote it out as an example so I don't know how it'll perform. Guess I'll have to do some more research about it :D

    Thank you for your help Nodsu!
  6. Nic

    Nic TechSpot Paladin Posts: 1,549

    With UDP you are not guaranteed to receive a response, and packets may arrive out of sequence. If you stop listening after "final" is received, there is a possibility that you may have packets missing.
  7. MrGaribaldi

    MrGaribaldi TechSpot Ambassador Topic Starter Posts: 2,512

    Thanks Nic! :) Didn't know that.

    Luckily I've found a site which covers a bit of server browser programming, and it has a short "guide" on how to receive multiple UDP packets...
    (At least it told me the contents of the UDP header ;))
Topic Status:
Not open for further replies.

Similar Topics

Add your comment to this article

You need to be a member to leave a comment. Join thousands of tech enthusiasts and participate.
TechSpot Account You may also...