Hi everyone, I am new to this forum so please excuse me if I have posted this in the wrong place. I have a problem that I need some advice on and any help would be appreciated.
In my NetCentric Computing course, we were asked to write a datagram socket program that allows the user to send and receive messages (in a blocked fashion that makes each user wait for a response before they can send again) that terminates both processes when either enters the keyword "bye". We were given some basic example code to work from that shows us how to either send or receive one message.
The problem I am having is with the Receive mode. I am using a do while loop to
test for "bye" like the instructor suggested. The method outlined by the teacher is to create a byte array called buffer to hold the datagram data, then pass that data into a
string using buffer as its argument. The problem is that the byte array, which is of fixed length MAX_LEN = 10, passes its entire contents into the string "message". If the previous transmission was "hello", and the current one is "bye", the array still holds the remainder of the previous message so the string "message" will get "byelo" passed into it.
So, I tried to clear the buffer using a for loop after it has passed its data into "message" and before printing "message" to screen and indeed that does correct the problem. I find it strange that doing so after I assigned "message" it as an argument would affect "message" in any way. But, when testing "message" the loop does not end even though message says "bye" (testing with do{....}while(!message.equals("bye"));). So I added a character counter loop to test the message and indeed, its length is 10, the same length of the byte array that I used as the argument for "message". My do while loop cannot work since the string has more characters than just "bye" even though message is not displaying them on the screen. So, logically, I tried using message.trim() to remove any white spaces before testing it and that had no effect on the situation (this was the teacher's recommendation).
This class is our first exposure to
Java programming in my major, so I am inexperienced with the syntax. It took me a lot of research to figure out a fix but according to my instructor, I shouldn't have needed it. What I did was to take the string "message" and passed its data into a stringBuffer object. Next I used datagram.getLength() to get the actual length of the most recent datagram received and used it as an argument in my stringBuffer which I called "bufferFix". Finally, I passed that into a new string called "testMessage". "testMessage" then could be effectively tested, and I could now terminate using the data received. Quite a process to get the job done but was the only way I could get it to work. After four days with little sleep trying to get done before the deadline, I was quite frustrated that I had to go through this just to get the example code to work correctly. And the teacher said I should not have needed it.
Here is the method I used for receiving in the datagramSocket class we were given to work with, including my corrections. If anyone can see where I went wrong and why the byte array was screwing up the message, I would appreciate it. My understanding of arrays which comes from C++ is that the array should still contain whatever was left over from the last entry that the current one did not overwrite.
Note: My code does work correctly I am just confused about why I had to make these modifications. The text and the Java docs both use the byte array for "message" so why can't I get it to work that way? Our next lab requires us to write a send/receive program using a client/server model and threading so that communication takes place concurrently. I'd like to know what the problem is with this one before tackling that project.
Thanks for any opinions, I just want to understand what I did wrong.