我有一个场景,当我的多播侦听器加入组时,路由器可能会关闭。在那种情况下,多播消息将永远不会到达侦听器。
所以我打算让监听器超时,然后重新加入多播组。
问题是下面的代码并不能保证监听器成功注册并接收到多播消息。
final MulticastSocket mcSocket = new MulticastSocket(POR);
// Join group before router started
mcSocket.joingGroup(mcAddress);
// wait until router starts
Thread.sleep(LONG_TIME);
mcSocket.leaveGroup(mcAddress);
// Join group after router started.
// Expected that this would re-register listener with router, but it doesn't
mcSocket.joingGroup(mcAddress);
// packet is never received
mcSocket.receive(packet);
那么,我需要做些什么来确保侦听器重新注册到路由器?