File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Multicasting Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multicasting Problem" Watch "Multicasting Problem" New topic

Multicasting Problem

Vlad Bozic

Joined: Oct 31, 2003
Posts: 1
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

Joined: Mar 06, 2001
Posts: 13459

Try moving the byte array declaration inside th 'for' loop so it gets recreated each time rather than re-used.
I agree. Here's the link:
subject: Multicasting Problem
It's not a secret anymore!