コントローラーが単純なメッセージを介してステーションをロックおよびロック解除する、非常に単純なマルチキャスト アプリケーションを作成しています。コントローラーとステーションの両方にレシーバー スレッドがあります。何らかの理由で、最初のメッセージが送信されたときは正常に受信されますが、2 番目のメッセージが送信されると、最初のメッセージの一部が添付されて正しく受信されません。
例えば、
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"
ステーションの受信機は次のとおりです。
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;
}
}
}
そして、コントローラーのメッセージが送信される場所は次のとおりです。
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();
}
}
}
これはおそらく初心者の問題であることはわかっていますが、マルチキャストやネットワーク全般についてはあまり経験がありません。