Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Data unmarshalling (UDP)

    Hello,

    I am working on a server-client app but I am facing a problem.

    The problem is that I don't know how to do unmarshalling properly. As an example. The client sends a request to obtain a list of existing blog records from the server. The server connects to the DB, retrieves all the information and send it back to the client. Then I get the response at the client. I try to unmarshall the data but I don't know how the unmarshalling will "cut" each string properly.
    Lets say, 2 strings, user and title. On one case the length of each string might be 6, 13. In that way I will use :


    String user_text = new String(received_bytes, 0, 5, "US-ASCII"); and
    String title_text = new String(received_bytes, 6, received_bytes.length, "US-ASCII");
    What if now in the next entry the strings' length are 7 and 18?

    I know that the solution might be very simple...
    Any help would be appreciated.

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Right - so first of all I need a bit more detail.

    Is the data which is being sent from the server coming in a UDP datagram stream?

    If so - does it need to be? Could the desired solution be created using Serializable classes sent down a TCP socket? This would be very easy and solve all of these forms of problems?

    Give some more info - perhaps UDP is necessary, perhaps you have some constraints .

    Anyhow - in answer to your actual question you could do something to delimit individual fields, so the servers response could be like this :

    Code:
    firstname:ANDY ARMSTRONG/##title text:This is my title/##
    And then your java code could be like:

    Code:
    String total = new String(received_bytes,0,received_bytes.length,"US-ASCII");
    
    String[] tokens = total.split("/##");
    
    String user_text = token[0].split(":")[1];
    String title_text = token[1].split(":")[1];
    Treat that as pseudo code - didnt try compiling it but you get the idea. You could alternatively send the response back in XML or something which describes the document like that!

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks for the renegadeandy. I have found another way to do it.

  • #4
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Can I ask what it was!?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •