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.