TechSpot

Java: Receiving multiple UDP packets

By MrGaribaldi
Oct 9, 2004
Topic Status:
Not open for further replies.
  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: 9,431

    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,802

    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 :)
    Thanks
     
  4. Nodsu

    Nodsu TS Rookie Posts: 9,431

    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,802

    Sorry, I guess I've not been overly clear.
    Code:
                DatagramPacket responsePakke = new DatagramPacket(responseData, responseData.length);
                
                ds.connect(<adress>);
                ds.setSoTimeout(1000);
                ds.send(<serverQuery>);
                ds.receive(<serverResponse>);
                ds.close()
    
    Which is my basic code.

    Now this I can modify to either
    Code:
                ds.receive(<serverResponse1>);
                ds.receive(<serverResponse2>);
                ds.receive(<serverResponse...n>);
    
    up to the max amount of packets the server can return or
    Code:
                ds.receive(<serverResponse>);
                StringTokenizer st = new StringTokenizer(<serverResponse>);
                     if(!st.lastToken().equals("final"){
                             ds.receive(<serverRespons>);
                      }
    
    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,928

    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,802

    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.


Add New Comment

TechSpot Members
Login or sign up for free,
it takes about 30 seconds.
You may also...


Get complete access to the TechSpot community. Join thousands of technology enthusiasts that contribute and share knowledge in our forum. Get a private inbox, upload your own photo gallery and more.