Reputation: 817
I'm making a very simple multicast application where a controller locks and unlocks a station via simple messages. The controller and the station both have receiver threads. For some reason, when the first message is sent, it is received well, but when the second message is sent, it is received incorrectly, with some of the first message attached to it.
For example,
Station 1 sends a "Locked: 1001" message.
The controller receives this message correctly.
The controller sends a "Unlock: 1001" message.
Station 1 receives something like "Unlock: 1ocked: 1001"
Here's the station's receiver:
public class VotingStationReceiver implements Runnable{
private DummyVotingStation votingStation;
private MulticastSocket s;
private Thread listener = new Thread(this);
public VotingStationReceiver(MulticastSocket s, DummyVotingStation votingStation){
this.s = s;
this.votingStation = votingStation;
listener.start();
}
public void run() {
byte[] buf = new byte[1024];
while(true)
try {
DatagramPacket pack = new DatagramPacket(buf, buf.length);
s.receive(pack);
String msg = "";
msg = new String(pack.getData());
msg = msg.trim();
System.out.println(msg);
System.out.println("Voting Station: message received");
votingStation.processMessage(msg);
} catch(IOException e) {
break;
}
}
}
And here's where the controller's message is sent:
private String unlockMsg = "Unlock: ";
public void unlockStation(int lockedID) {
//send packet telling station to unlock
String completeMsg = unlockMsg+lockedID;
byte buf[] = completeMsg.getBytes();
// Create and send the DatagramPacket
DatagramPacket pack;
try {
pack = new DatagramPacket(buf, buf.length,
InetAddress.getByName(group), port);
int ttl = s.getTimeToLive();
s.setTimeToLive(ttl);
s.send(pack);
System.out.println("Control Station: message sent");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
I know this is probably a beginner's problem, but I don't have much experience with multicasting and networking in general.
Upvotes: 0
Views: 117
Reputation: 719336
The problem is that you are not paying attention to the length of the packet that you received. Change
msg = new String(pack.getData());
to
msg = new String(pack.getData(), 0, pack.getLength());
The first time you create a String in your code, it (kind of) works .... because you then trim()
the string to get rid of the trailing NUL (zero) characters.
The second time, you are reading into a dirty buffer, so the junk is decoding as non-NUL characters.
FWIW, if you make use of the packet length when creating the String, it is no longer necessary to trim()
it to get rid of trailing NUL characters.
Upvotes: 1
Reputation: 272367
I suspect your problem is that you're receiving a Datagram
into an existing buffer that's had data in it from a previous Datagram
receipt. Try using a new byte[]
for each receive. I think also you need to consider the length of data coming in and consequently check out this SO answer to make use of pack.getLength()
Upvotes: 0