Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multicasting Problem  RSS feed

Vlad Bozic
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
G'day people )
Since I am a newbie here and this is my first post here, please dont kick me out just yet.
I have just started learning Java, from lots of samples, and from books and I have been interested in the topic of multitasking. The snippet bellow allows me to send the message to the group, but when I get reply from someone in the group, I can see the previous message. Foe example, lets say Joe sends me a string "Have a nice day", and I display it on the screen, and then when some other Joe sends me a string i.e. "-----bye", the display is "-----byeice", so the first part of the original string gets replaced. I have tried empying the buffer, resetting couple other things around, but to no avail.
I am sure I am just missing a line or two, so any pointer in the right direction is more than appreciated.
byte[] buffer = new byte[1024];
for(;;) { // get messages from others in group, infinite loop
DatagramPacket messageIn = new DatagramPacket(buffer, buffer.length);

System.out.println("Received:" + new String(messageIn.getData()));

System.out.println ("Enter the message: ");
msg = inFromUser.readLine();
//By typing 'exit' user gets outta Multicast Group
if (msg.equals("exit"))
m = msg.getBytes ();
messageOut = new DatagramPacket(m, m.length, group, 6789);
Thanks a lot
David O'Meara
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try moving the byte array declaration inside th 'for' loop so it gets recreated each time rather than re-used.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!