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!
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.
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.
posted 1 week ago
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!!
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