Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Sending messages via UDP  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi  
i am slowly learning to code as an evening course.  We have an assignment to code a client application that takes an input string, splits into two halves and assigns them to packets.  These are sent to a server application that then takes the two halves, swaps them round and sends them back to front to the client.  The client then prints the results and confirms if the string returned matches the input string.  I have coded this and it kind of works.  I am stuck on one thing.  For some reason my byte arrays are not resetting after a message is sent from the server and what ends up happening is that it returns the characters from a new string and if this new string is shorter than old string the remaining characters are also returned.  What have i done wrong here?  I suspect it might be cos i have set the byte array on the server to 128 bits so just carries on till it runs out of memory?  If that is case i dont know how to assign a length to the array based on the incoming packet as i declare the array before the packet arrives!  Any advice would be great or indeed a topic i need to revisit in my studies that could explain the issue!

This is the client code:



This is the server code:



 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the server you don't check the length of data returned in the DatagramPacket.  There's a getLength method for that.
That tells you how much of the byte[] has been filled.  I think it can be larger than the size of the array, which means the data has been truncated.
 
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You're doing way way WAY too much in the main methods. Right now the main method of your client is responsible for all of:

  • reading input from the console,
  • splitting the input,
  • making two strings out of the split input,
  • serializing the phrases to binary,
  • creating a socket connection with the server,
  • sending packets to the server, and
  • receiving replies from the server.

  • The only place where you used another method was to check if the response contains the expected result. Why?

    Object oriented programming is about separating your concerns and making different classes responsible for different things. You want your main method to look something like this:

    You will want to do something similar for your server side.

    When you have done this, you can easily test which of the methods or classes that you've created is responsible for the problem.
     
    Safi Habib
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the advice...i did think about methods but the lecturer said for this exercise we can get away with it all in the main method, thats why it looks like this!

    I have corrected it so it checks the length and also my lecturer advised that the order of things happening needed to be looked at so i have changed that so that it makes more sense.

    I have edited my code and it works now but strangely only with long strings.  For example "Networking" and "Assignment" work ok, it returns both halves and checks they match.
    But when i type "hello" it only returns "lo" and "he" which off course fails the check that it does.

    Why is this happening?!

    **Edit** it seems to work only on strings that are even in length, not odd.  Not sure why but something is happening when i build the string with odd lengths that means an empty element is being sent!!

    Client code:






    Server code:

     
    Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
    global solutions you can do at home or in your backyard
    https://www.kickstarter.com/projects/paulwheaton/better-world-boo
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!